From ab1307c63b08274cb735947a4a4ca05c10fc01e9 Mon Sep 17 00:00:00 2001 From: Daniel Rainer Date: Sat, 21 Jun 2025 22:19:27 +0200 Subject: [PATCH] Do not `mmap` with len 0 `mmap` should fail when the length argument is 0. Checking this in advance allows returning early, without performing unnecessary syscalls. This also fixes an issue observed on FreeBSD 13.2 where `mmap` does not always fail when the length is 0, resulting in the `assert!(len > 0)` in `MmapRegion::new` failing. https://github.com/fish-shell/fish-shell/issues/11595 --- src/history/file.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/history/file.rs b/src/history/file.rs index 1215f5583..0dfb63ae3 100644 --- a/src/history/file.rs +++ b/src/history/file.rs @@ -128,6 +128,12 @@ pub fn create(mut history_file: &File) -> std::io::Result { )) } }; + if len == 0 { + return Err(std::io::Error::new( + std::io::ErrorKind::Other, + "History file is empty. Cannot create memory mapping with length 0.", + )); + } let map_anon = |mut file: &File, len: usize| -> std::io::Result { let mut region = MmapRegion::map_anon(len)?; // If we mapped anonymous memory, we have to read from the file.