From 05f82ba25225838749fa3029f77e91b546e44667 Mon Sep 17 00:00:00 2001 From: Ben Langmuir Date: Thu, 1 May 2014 03:33:36 +0000 Subject: 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 --- clang/lib/Serialization/ModuleManager.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'clang/lib/Serialization/ModuleManager.cpp') diff --git a/clang/lib/Serialization/ModuleManager.cpp b/clang/lib/Serialization/ModuleManager.cpp index c36d902..9b3159e1 100644 --- a/clang/lib/Serialization/ModuleManager.cpp +++ b/clang/lib/Serialization/ModuleManager.cpp @@ -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; } -- cgit v1.1