Check that a file is not a directory before reading it into a MemoryBuffer.
authorKaelyn Uhrain <rikka@google.com>
Wed, 20 Jun 2012 20:21:33 +0000 (20:21 +0000)
committerKaelyn Uhrain <rikka@google.com>
Wed, 20 Jun 2012 20:21:33 +0000 (20:21 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@158841 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/MemoryBuffer.cpp

index 16e5c7a9f72bd7d4271aadbc50b33a12207e8f8f..fabf1406acbdb3c0e90b9a85cb67add0b36578c8 100644 (file)
@@ -17,6 +17,7 @@
 #include "llvm/Config/config.h"
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/Errno.h"
+#include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/Process.h"
 #include "llvm/Support/Program.h"
@@ -214,6 +215,14 @@ error_code MemoryBuffer::getFile(const char *Filename,
                                  OwningPtr<MemoryBuffer> &result,
                                  int64_t FileSize,
                                  bool RequiresNullTerminator) {
+  // First check that the "file" is not a directory
+  bool is_dir = false;
+  error_code err = sys::fs::is_directory(Filename, is_dir);
+  if (err)
+    return err;
+  else if (is_dir)
+    return make_error_code(errc::is_a_directory);
+
   int OpenFlags = O_RDONLY;
 #ifdef O_BINARY
   OpenFlags |= O_BINARY;  // Open input file in binary mode on win32.