Avoid a potential race between stat() and open() of ASTFile

We need to open an ASTFile while checking its expected size and
modification time, or another clang instance can modify the file between
the stat() and the open().

llvm-svn: 207735
This commit is contained in:
Ben Langmuir 2014-05-01 03:33:36 +00:00
parent 7d6d2705f6
commit 05f82ba252
1 changed files with 5 additions and 1 deletions

View File

@ -381,7 +381,9 @@ bool ModuleManager::lookupModuleFile(StringRef FileName,
off_t ExpectedSize,
time_t ExpectedModTime,
const FileEntry *&File) {
File = FileMgr.getFile(FileName, /*openFile=*/false, /*cacheFailure=*/false);
// Open the file immediately to ensure there is no race between stat'ing and
// opening the file.
File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false);
if (!File && FileName != "-") {
return false;
@ -389,6 +391,8 @@ bool ModuleManager::lookupModuleFile(StringRef FileName,
if ((ExpectedSize && ExpectedSize != File->getSize()) ||
(ExpectedModTime && ExpectedModTime != File->getModificationTime())) {
FileMgr.invalidateCache(File);
File = nullptr;
return true;
}