1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

truncate: prevent underflow when reducing size

Prevent usize underflow when reducing the size of a file by more than
its current size. For example, if `f` is a file with 3 bytes, then

    truncate -s-5 f

will now set the size of the file to 0 instead of causing a panic.
This commit is contained in:
Jeffrey Finkelstein 2022-01-28 22:44:07 -05:00
parent f1d72018d7
commit 0454d3b243
2 changed files with 40 additions and 1 deletions

View file

@ -377,3 +377,15 @@ fn test_division_by_zero_reference_and_size() {
.no_stdout()
.stderr_contains("division by zero");
}
/// Test that truncate with a relative size less than 0 is not an error.
#[test]
fn test_underflow_relative_size() {
let (at, mut ucmd) = at_and_ucmd!();
ucmd.args(&["-s-1", FILE1])
.succeeds()
.no_stdout()
.no_stderr();
assert!(at.file_exists(FILE1));
assert!(at.read_bytes(FILE1).is_empty());
}