aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2010-11-23 21:53:56 +0000
committerChris Lattner <sabre@nondot.org>2010-11-23 21:53:56 +0000
commitb2b93c12d768cfdeb21909f734b04ccb82d75da2 (patch)
tree5b0fd5ce3be2ee59d6b3276ae6a9d1dc1cec8a99
parent291fcf02980be85cf13c8a63bb036a19012311df (diff)
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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@120062 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/Basic/FileSystemStatCache.cpp23
1 files changed, 22 insertions, 1 deletions
diff --git a/lib/Basic/FileSystemStatCache.cpp b/lib/Basic/FileSystemStatCache.cpp
index b2d43e4744..c6b11e9024 100644
--- a/lib/Basic/FileSystemStatCache.cpp
+++ b/lib/Basic/FileSystemStatCache.cpp
@@ -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 @@ bool FileSystemStatCache::get(const char *Path, struct stat &StatBuf,
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;
}