[llvm-commits] [llvm] r154082 - /llvm/trunk/lib/Support/MemoryBuffer.cpp

Argyrios Kyrtzidis akyrtzi at gmail.com
Wed Apr 4 21:23:56 PDT 2012


Author: akirtzidis
Date: Wed Apr  4 23:23:56 2012
New Revision: 154082

URL: http://llvm.org/viewvc/llvm-project?rev=154082&view=rev
Log:
In MemoryBuffer::getOpenFile() make sure that the buffer is null-terminated if
the caller requested a null-terminated one.

When mapping the file there could be a racing issue that resulted in the file being larger
than the FileSize passed by the caller. We already have an assertion
for this in MemoryBuffer::init() but have a runtime guarantee that
the buffer will be null-terminated, so do a copy that adds a null-terminator.

Protects against crash of rdar://11161822.

Modified:
    llvm/trunk/lib/Support/MemoryBuffer.cpp

Modified: llvm/trunk/lib/Support/MemoryBuffer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/MemoryBuffer.cpp?rev=154082&r1=154081&r2=154082&view=diff
==============================================================================
--- llvm/trunk/lib/Support/MemoryBuffer.cpp (original)
+++ llvm/trunk/lib/Support/MemoryBuffer.cpp Wed Apr  4 23:23:56 2012
@@ -304,6 +304,16 @@
                                                       RealMapOffset)) {
       result.reset(GetNamedBuffer<MemoryBufferMMapFile>(
           StringRef(Pages + Delta, MapSize), Filename, RequiresNullTerminator));
+
+      if (RequiresNullTerminator && result->getBufferEnd()[0] != '\0') {
+        // There could be a racing issue that resulted in the file being larger
+        // than the FileSize passed by the caller. We already have an assertion
+        // for this in MemoryBuffer::init() but have a runtime guarantee that
+        // the buffer will be null-terminated here, so do a copy that adds a
+        // null-terminator.
+        result.reset(MemoryBuffer::getMemBufferCopy(result->getBuffer(),
+                                                    Filename));
+      }
       return error_code::success();
     }
   }
@@ -339,6 +349,7 @@
     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;





More information about the llvm-commits mailing list