Skip to content

Commit

Permalink
stat: handle error better. should make tests/stat/stat-printf.pl pass
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvestre committed Dec 7, 2024
1 parent b7fc62e commit 07d6056
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/uu/stat/src/stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,15 @@ impl Stater {
if let Some((field_width, offset)) = format_str[j..].scan_num::<usize>() {
width = field_width;
j += offset;

// Reject directives like `%<NUMBER>` by checking if width has been parsed.
if j >= bound || chars[j] == '%' {
let invalid_directive: String = chars[old..=j.min(bound - 1)].iter().collect();
return Err(USimpleError::new(
1,
format!("{}: invalid directive", invalid_directive.quote()),
));
}
}
check_bound(format_str, bound, old, j)?;

Expand Down
17 changes: 17 additions & 0 deletions tests/by-util/test_stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,20 @@ fn test_printf_bel_etc() {
.succeeds()
.stdout_is_bytes(expected_stdout);
}

#[test]
fn test_printf_invalid_directive() {
let ts = TestScenario::new(util_name!());

ts.ucmd()
.args(&["--printf=%9", "."])
.fails()
.code_is(1)
.stderr_contains("'%9': invalid directive");

ts.ucmd()
.args(&["--printf=%9%", "."])
.fails()
.code_is(1)
.stderr_contains("'%9%': invalid directive");
}

0 comments on commit 07d6056

Please sign in to comment.