[cfe-commits] r120062 - /cfe/trunk/lib/Basic/FileSystemStatCache.cpp

Chris Lattner sabre at nondot.org
Tue Nov 23 13:53:56 PST 2010


Author: lattner
Date: Tue Nov 23 15:53:56 2010
New Revision: 120062

URL: http://llvm.org/viewvc/llvm-project?rev=120062&view=rev
Log:
if we succeed in opening a directory but expected a file, ensure we don't
leak a filedescriptor if a client ever starts returning one.

Modified:
    cfe/trunk/lib/Basic/FileSystemStatCache.cpp

Modified: cfe/trunk/lib/Basic/FileSystemStatCache.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/FileSystemStatCache.cpp?rev=120062&r1=120061&r2=120062&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/FileSystemStatCache.cpp (original)
+++ cfe/trunk/lib/Basic/FileSystemStatCache.cpp Tue Nov 23 15:53:56 2010
@@ -13,6 +13,14 @@
 
 #include "clang/Basic/FileSystemStatCache.h"
 #include "llvm/System/Path.h"
+
+// FIXME: This is terrible, we need this for ::close.
+#if !defined(_MSC_VER) && !defined(__MINGW32__)
+#include <unistd.h>
+#include <sys/uio.h>
+#else
+#include <io.h>
+#endif
 using namespace clang;
 
 #if defined(_MSC_VER)
@@ -37,10 +45,23 @@
   else
     R = ::stat(Path, &StatBuf) != 0 ? CacheMissing : CacheExists;
 
+  // If the path doesn't exist, return failure.
   if (R == CacheMissing) return true;
   
+  // If the path exists, make sure that its "directoryness" matches the clients
+  // demands.
   bool isForDir = FileDescriptor == 0;
-  return S_ISDIR(StatBuf.st_mode) != isForDir;
+  if (S_ISDIR(StatBuf.st_mode) != isForDir) {
+    // If not, close the file if opened.
+    if (FileDescriptor && *FileDescriptor != -1) {
+      ::close(*FileDescriptor);
+      *FileDescriptor = -1;
+    }
+    
+    return true;
+  }
+  
+  return false;
 }
 
 





More information about the cfe-commits mailing list