diff options
author | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2014-05-06 00:51:45 +0000 |
---|---|---|
committer | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2014-05-06 00:51:45 +0000 |
commit | bde59274bb1e213ed76e27c0fc69c23ef99b0b37 (patch) | |
tree | 7eb8b19edbb0f476baf3448bc62975ca6ddf9021 /llvm/lib/Support/MemoryBuffer.cpp | |
parent | 8620d933f4e19a014fccbfe71b16be60fffe8a27 (diff) | |
download | llvm-bde59274bb1e213ed76e27c0fc69c23ef99b0b37.zip llvm-bde59274bb1e213ed76e27c0fc69c23ef99b0b37.tar.gz llvm-bde59274bb1e213ed76e27c0fc69c23ef99b0b37.tar.bz2 |
[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.
llvm-svn: 208021
Diffstat (limited to 'llvm/lib/Support/MemoryBuffer.cpp')
-rw-r--r-- | llvm/lib/Support/MemoryBuffer.cpp | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/llvm/lib/Support/MemoryBuffer.cpp b/llvm/lib/Support/MemoryBuffer.cpp index dc615d2..a42138c 100644 --- a/llvm/lib/Support/MemoryBuffer.cpp +++ b/llvm/lib/Support/MemoryBuffer.cpp @@ -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, const char *Filename, 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, const char *Filename, 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; |