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
17 changes: 11 additions & 6 deletions src/find/matchers/printf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl FormatStringParser<'_> {
}
}

fn parse_format_width(&mut self) -> Option<usize> {
fn parse_format_width(&mut self) -> Result<Option<usize>, Box<dyn Error>> {
let start = self.string;
let mut digits = 0;

Expand All @@ -218,11 +218,16 @@ impl FormatStringParser<'_> {
}

if digits > 0 {
// safe to unwrap: we already know all the digits are valid due to
// the above checks.
Some((start[0..digits]).parse().unwrap())
let digits = &start[0..digits];
let width: usize = digits
.parse()
.map_err(|_| format!("Invalid format width: {digits}"))?;
if width > u16::MAX as usize {
return Err(format!("Format width too large: {digits}").into());
}
Ok(Some(width))
} else {
None
Ok(None)
}
}

Expand Down Expand Up @@ -258,7 +263,7 @@ impl FormatStringParser<'_> {
self.advance_one().unwrap();
}

let width = self.parse_format_width();
let width = self.parse_format_width()?;

let first = self.advance_one()?;
if first == '%' {
Expand Down
24 changes: 24 additions & 0 deletions tests/test_find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,30 @@ fn find_printf_octal_escape_before_multibyte_char() {
.stdout_only("\0€\n");
}

#[test]
fn find_printf_width_too_large() {
ucmd()
.args(&[
"./test_data/simple",
"-maxdepth",
"0",
"-printf",
"%70000s\\n",
])
.fails()
.stderr_contains("find: Format width too large");
ucmd()
.args(&[
"./test_data/simple",
"-maxdepth",
"0",
"-printf",
"%99999999999999999999s\\n",
])
.fails()
.stderr_contains("find: Invalid format width");
}

#[cfg(unix)]
#[test]
fn find_perm() {
Expand Down
Loading