summaryrefslogtreecommitdiffstats
path: root/clang
diff options
context:
space:
mode:
authorIlya Biryukov <ibiryukov@google.com>2017-08-10 16:10:40 +0000
committerIlya Biryukov <ibiryukov@google.com>2017-08-10 16:10:40 +0000
commitb88de416367d703eff36c87722537658f2aaee0f (patch)
treef71bc61b0de828ec3446e76323d41c777ea1bf88 /clang
parent9c77d27004a1de70f5a0d7b5fffaca470d09177a (diff)
downloadbcm5719-llvm-b88de416367d703eff36c87722537658f2aaee0f.tar.gz
bcm5719-llvm-b88de416367d703eff36c87722537658f2aaee0f.zip
Fixed a race condition in PrecompiledPreamble.
Summary: Two PrecompiledPreambles, used in parallel on separate threads, could be writing preamble to the same temporary file. Reviewers: bkramer, krasimir, klimek Reviewed By: klimek Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D36529 llvm-svn: 310618
Diffstat (limited to 'clang')
-rw-r--r--clang/lib/Frontend/PrecompiledPreamble.cpp10
1 files changed, 9 insertions, 1 deletions
diff --git a/clang/lib/Frontend/PrecompiledPreamble.cpp b/clang/lib/Frontend/PrecompiledPreamble.cpp
index 15b24cbed48..cd7446189cc 100644
--- a/clang/lib/Frontend/PrecompiledPreamble.cpp
+++ b/clang/lib/Frontend/PrecompiledPreamble.cpp
@@ -28,6 +28,7 @@
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Mutex.h"
#include "llvm/Support/MutexGuard.h"
+#include "llvm/Support/Process.h"
using namespace clang;
@@ -462,9 +463,16 @@ llvm::ErrorOr<PrecompiledPreamble::TempPCHFile>
PrecompiledPreamble::TempPCHFile::createInSystemTempDir(const Twine &Prefix,
StringRef Suffix) {
llvm::SmallString<64> File;
- auto EC = llvm::sys::fs::createTemporaryFile(Prefix, Suffix, /*ref*/ File);
+ // Using a version of createTemporaryFile with a file descriptor guarantees
+ // that we would never get a race condition in a multi-threaded setting (i.e.,
+ // multiple threads getting the same temporary path).
+ int FD;
+ auto EC = llvm::sys::fs::createTemporaryFile(Prefix, Suffix, /*ref*/ FD,
+ /*ref*/ File);
if (EC)
return EC;
+ // We only needed to make sure the file exists, close the file right away.
+ llvm::sys::Process::SafelyCloseFileDescriptor(FD);
return TempPCHFile(std::move(File).str());
}
OpenPOWER on IntegriCloud