Skip to content

Commit ca4f219

Browse files
author
git apple-llvm automerger
committed
Merge commit 'af18c6f22e43' from llvm.org/release/21.x into stable/21.x
2 parents 01b9de1 + af18c6f commit ca4f219

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

llvm/lib/Support/MemoryBuffer.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,14 @@ getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize,
501501
std::unique_ptr<MB> Result(
502502
new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile<MB>(
503503
RequiresNullTerminator, FD, MapSize, Offset, EC));
504-
if (!EC)
505-
return std::move(Result);
504+
if (!EC) {
505+
// On at least Linux, and possibly on other systems, mmap may return pages
506+
// from the page cache that are not properly filled with trailing zeroes,
507+
// if some prior user of the page wrote non-zero bytes. Detect this and
508+
// don't use mmap in that case.
509+
if (!RequiresNullTerminator || *Result->getBufferEnd() == '\0')
510+
return std::move(Result);
511+
}
506512
}
507513

508514
#ifdef __MVS__

0 commit comments

Comments
 (0)