]> granicus.if.org Git - clang/commitdiff
Avoid a potential race between stat() and open() of ASTFile
authorBen Langmuir <blangmuir@apple.com>
Thu, 1 May 2014 03:33:36 +0000 (03:33 +0000)
committerBen Langmuir <blangmuir@apple.com>
Thu, 1 May 2014 03:33:36 +0000 (03:33 +0000)
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().

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@207735 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Serialization/ModuleManager.cpp

index c36d902deb7f651a207ca67aa4ea9d86bb36c163..9b3159e1705af610788e7e682f4f72fc60fe68fb 100644 (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;
   }