diff options
author | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2012-04-05 04:23:56 +0000 |
---|---|---|
committer | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2012-04-05 04:23:56 +0000 |
commit | 541b2a4aa3401b1dcff9a127e0abeee08c5720f9 (patch) | |
tree | 39f35490f6df9fca18a01dbace65ce296f9b22eb /lib/Support | |
parent | b657a90929867716ca1c7c12d442bb5d32281bd4 (diff) | |
download | external_llvm-541b2a4aa3401b1dcff9a127e0abeee08c5720f9.zip external_llvm-541b2a4aa3401b1dcff9a127e0abeee08c5720f9.tar.gz external_llvm-541b2a4aa3401b1dcff9a127e0abeee08c5720f9.tar.bz2 |
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.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@154082 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support')
-rw-r--r-- | lib/Support/MemoryBuffer.cpp | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/lib/Support/MemoryBuffer.cpp b/lib/Support/MemoryBuffer.cpp index 911a03f..16e5c7a 100644 --- a/lib/Support/MemoryBuffer.cpp +++ b/lib/Support/MemoryBuffer.cpp @@ -304,6 +304,16 @@ error_code MemoryBuffer::getOpenFile(int FD, const char *Filename, 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 @@ 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; |