[llvm] r208021 - [Support/MemoryBuffer] Move the IsVolatile check inside shouldUseMmap() and make sure to zero-initialize the rest
Argyrios Kyrtzidis
akyrtzi at gmail.com
Mon May 5 17:51:45 PDT 2014
Author: akirtzidis
Date: Mon May 5 19:51:45 2014
New Revision: 208021
URL: http://llvm.org/viewvc/llvm-project?rev=208021&view=rev
Log:
[Support/MemoryBuffer] Move the IsVolatile check inside shouldUseMmap() and make sure to zero-initialize the rest
of the buffer if we unexpectedly reach end-of-file while reading.
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=208021&r1=208020&r2=208021&view=diff
==============================================================================
--- llvm/trunk/lib/Support/MemoryBuffer.cpp (original)
+++ llvm/trunk/lib/Support/MemoryBuffer.cpp Mon May 5 19:51:45 2014
@@ -305,7 +305,14 @@ static bool shouldUseMmap(int FD,
size_t MapSize,
off_t Offset,
bool RequiresNullTerminator,
- int PageSize) {
+ int PageSize,
+ bool IsVolatile) {
+ // mmap may leave the buffer without null terminator if the file size changed
+ // by the time the last page is mapped in, so avoid it if the file size is
+ // likely to change.
+ if (IsVolatile)
+ return false;
+
// We don't use mmap for small files because this can severely fragment our
// address space.
if (MapSize < 4 * 4096 || MapSize < (unsigned)PageSize)
@@ -381,9 +388,8 @@ static error_code getOpenFileImpl(int FD
MapSize = FileSize;
}
- if (!IsVolatile &&
- shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator,
- PageSize)) {
+ if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator,
+ PageSize, IsVolatile)) {
error_code EC;
Result.reset(new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile(
RequiresNullTerminator, FD, MapSize, Offset, EC));
@@ -420,9 +426,9 @@ static error_code getOpenFileImpl(int FD
return error_code(errno, posix_category());
}
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.
+ assert(IsVolatile && "We got inaccurate FileSize value or fstat reported "
+ "an invalid file size.");
+ memset(BufPtr, 0, BytesLeft); // zero-initialize rest of the buffer.
break;
}
BytesLeft -= NumRead;
More information about the llvm-commits
mailing list