Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/uu/tac/src/tac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,9 +451,19 @@ fn buffer_stdin() -> std::io::Result<StdinData> {
}

fn try_mmap_file(file: &File) -> Option<Mmap> {
// SAFETY: If the file is truncated while we map it, SIGBUS will be raised
// and our process will be terminated, thus preventing access of invalid memory.
unsafe { Mmap::map(file).ok() }
// Use make_read_only() on a MmapMut created with MAP_PRIVATE.
// With MAP_PRIVATE, truncation of the underlying file won't cause
// SIGBUS because the kernel serves zero-filled pages for the
// truncated region instead of faulting. This is safer than the
// default MAP_SHARED from Mmap::map().
//
// See: https://github.com/uutils/coreutils/issues/9748
let mmap_mut = unsafe {
memmap2::MmapOptions::new()
.map_copy_read_only(file)
.ok()?
};
Some(mmap_mut)
}

#[cfg(test)]
Expand Down
Loading