expression

Check file length and account for truncation hazards

Access to a mapped page beyond the end of a truncated file can cause SIGBUS on Linux. Validate the range and prevent outside truncation for the entire use of the mapping. A length check is necessary for this model but cannot prevent later races. Expected stdout (not executed): initial: true after shrink: false Common mistake: Assuming bounds checks on a Rust slice protect against a backing file being shortened by another process. Run only in the isolated fixture runner, not a public code playground.

Formal Syntax & Structure

rust syntaxrust
offset.checked_add(len).is_some_and(|end| end <= file_len)

Working Example

Example Snippetrust
fn fits(offset: usize, len: usize, file_len: usize) -> bool {    len != 0 && offset.checked_add(len).is_some_and(|end| end <= file_len)}fn main() {    println!("initial: {}", fits(4, 4, 8));    println!("after shrink: {}", fits(4, 4, 6));}
Rust Reference