LLVM Bug Fix 13709: Remove needless lsr(Rp, #32) instruction access the
[oota-llvm.git] / lib / Support / MemoryBuffer.cpp
index 911a03f8088bd70f97352b36352f254e68c47d2b..992f03c52058b9de1341fb00abd7ee9635c97e5e 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;
+  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.
@@ -339,6 +348,7 @@ error_code MemoryBuffer::getOpenFile(int FD, const char *Filename,
     if (NumRead == 0) {
       assert(0 && "We got inaccurate FileSize value or fstat reported an "
                    "invalid file size.");
+      *BufPtr = '\0'; // null-terminate at the actual size.
       break;
     }
     BytesLeft -= NumRead;