Skip to content

Commit 07d6056

Browse files
committed
stat: handle error better. should make tests/stat/stat-printf.pl pass
1 parent b7fc62e commit 07d6056

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/uu/stat/src/stat.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,15 @@ impl Stater {
563563
if let Some((field_width, offset)) = format_str[j..].scan_num::<usize>() {
564564
width = field_width;
565565
j += offset;
566+
567+
// Reject directives like `%<NUMBER>` by checking if width has been parsed.
568+
if j >= bound || chars[j] == '%' {
569+
let invalid_directive: String = chars[old..=j.min(bound - 1)].iter().collect();
570+
return Err(USimpleError::new(
571+
1,
572+
format!("{}: invalid directive", invalid_directive.quote()),
573+
));
574+
}
566575
}
567576
check_bound(format_str, bound, old, j)?;
568577

tests/by-util/test_stat.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,3 +412,20 @@ fn test_printf_bel_etc() {
412412
.succeeds()
413413
.stdout_is_bytes(expected_stdout);
414414
}
415+
416+
#[test]
417+
fn test_printf_invalid_directive() {
418+
let ts = TestScenario::new(util_name!());
419+
420+
ts.ucmd()
421+
.args(&["--printf=%9", "."])
422+
.fails()
423+
.code_is(1)
424+
.stderr_contains("'%9': invalid directive");
425+
426+
ts.ucmd()
427+
.args(&["--printf=%9%", "."])
428+
.fails()
429+
.code_is(1)
430+
.stderr_contains("'%9%': invalid directive");
431+
}

0 commit comments

Comments
 (0)