aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Support/MemoryBuffer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Support/MemoryBuffer.cpp')
-rw-r--r--lib/Support/MemoryBuffer.cpp11
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;