Skip to content
Merged
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion src/uu/truncate/src/truncate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,11 @@ fn parse_mode_and_size(size_string: &str) -> Result<TruncateMode, ParseSizeError
// Get the modifier character from the size string, if any. For
// example, if the argument is "+123", then the modifier is '+'.
if let Some(c) = size_string.chars().next() {
// Check if there's a non-numerical string after the positive/negative sign
if (matches!(c, '+' | '-')) && !size_string.chars().nth(1).unwrap_or(c).is_ascii_digit() {
return Err(ParseSizeError::ParseFailure(format!("'{size_string}'")));
}

if is_modifier(c) {
size_string = &size_string[1..];
}
Expand All @@ -378,7 +383,7 @@ fn parse_mode_and_size(size_string: &str) -> Result<TruncateMode, ParseSizeError
_ => TruncateMode::Absolute,
})
} else {
Err(ParseSizeError::ParseFailure(size_string.to_string()))
Err(ParseSizeError::ParseFailure("''".to_string()))
}
}

Expand Down
16 changes: 16 additions & 0 deletions tests/by-util/test_truncate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,3 +491,19 @@ fn test_truncate_non_utf8_paths() {
// Test that truncate can handle non-UTF-8 filenames
ts.ucmd().arg("-s").arg("10").arg(file_name).succeeds();
}

#[test]
fn test_empty_size() {
new_ucmd!()
.args(&["-s", "", "asd"])
.fails()
.stderr_is("truncate: Invalid number: ''\n");
}

#[test]
fn test_sign_as_a_size() {
new_ucmd!()
.args(&["-s", "+", "asd"])
.fails()
.stderr_is("truncate: Invalid number: '+'\n");
}
Loading