Skip to content
Open
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
5 changes: 4 additions & 1 deletion src/uu/stat/src/stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,13 @@ struct Flags {
/// where `beg` & `end` is the beginning and end index of sub-string, respectively
fn check_bound(slice: &str, bound: usize, beg: usize, end: usize) -> UResult<()> {
if end >= bound {
// `beg`/`end` are char indices, so take the directive by chars: byte-slicing
// `slice` could land mid-UTF-8 when a multibyte char precedes the directive.
let directive: String = slice.chars().skip(beg).take(end - beg).collect();
return Err(USimpleError::new(
1,
StatError::InvalidDirective {
directive: slice[beg..end].quote().to_string(),
directive: directive.quote().to_string(),
}
.to_string(),
));
Expand Down
11 changes: 11 additions & 0 deletions tests/by-util/test_stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,17 @@ fn test_printf_invalid_directive() {
.stderr_contains("'%9%': invalid directive");
}

#[test]
fn test_invalid_directive_after_multibyte_char() {
let ts = TestScenario::new(util_name!());
for (fmt, directive) in [("€%-", "%-"), ("ä%0", "%0"), ("€%.", "%.")] {
ts.ucmd()
.args(&["-c", fmt, "."])
.fails_with_code(1)
.stderr_only(format!("stat: '{directive}': invalid directive\n"));
}
}

#[test]
#[cfg(all(
feature = "feat_selinux",
Expand Down
Loading