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
32 changes: 13 additions & 19 deletions src/uu/tac/src/tac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,20 +383,19 @@ fn tac(filenames: &[OsString], before: bool, regex: bool, separator: &OsStr) ->
}
};

if let Some(mmap1) = try_mmap_file(&file) {
mmap = mmap1;
&mmap
} else {
let mut contents = Vec::new();
match file.read_to_end(&mut contents) {
Ok(_) => {
buf = contents;
&buf
}
Err(e) => {
show!(TacError::ReadError(filename.clone(), e));
continue;
}
// Read the file into memory instead of memory-mapping it.
// Using mmap on regular files is unsafe because if the file is
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Using mmap on regular files is unsafe because if the file is
// Using mmap on regular files is unsound because if the file is

// truncated while mapped, accessing the now-invalid region
// triggers SIGBUS and crashes the process (see #9748).
let mut contents = Vec::new();
match file.read_to_end(&mut contents) {
Ok(_) => {
buf = contents;
&buf
}
Err(e) => {
show!(TacError::ReadError(filename.clone(), e));
continue;
}
}
};
Expand Down Expand Up @@ -450,11 +449,6 @@ 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() }
}

#[cfg(test)]
mod tests_hybrid_flavor {
Expand Down
Loading