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

fn advance_one(&mut self) -> Result<char, Box<dyn Error>> {
let c = self.front()?;
self.string = &self.string[1..];
self.string = &self.string[c.len_utf8()..];
Ok(c)
}

Expand Down Expand Up @@ -203,7 +203,10 @@ impl FormatStringParser<'_> {
'v' => "\x0B",
'0' => "\0",
'\\' => "\\",
c => return Err(format!("Invalid escape sequence: \\{c}").into()),
c => {
eprintln!("find: warning: unrecognized escape '\\{c}'");
return Ok(FormatComponent::Literal(format!("\\{c}")));
}
};

Ok(FormatComponent::Literal(c.to_string()))
Expand Down Expand Up @@ -312,7 +315,10 @@ impl FormatStringParser<'_> {
},
'Y' => FormatDirective::Type { follow_links: true },
// TODO: %Z
_ => return Ok(FormatComponent::Literal(first.to_string())),
_ => {
eprintln!("find: warning: unrecognized format directive '%{first}'");
return Ok(FormatComponent::Literal(format!("%{first}")));
}
};

Ok(FormatComponent::Directive {
Expand Down Expand Up @@ -698,7 +704,12 @@ mod tests {
]
);

assert!(FormatString::parse("\\X").is_err());
// An unrecognized escape is a warning, not an error: it's printed
// literally (backslash included), matching GNU find.
assert_eq!(
FormatString::parse("\\X").unwrap().components,
vec![FormatComponent::Literal("\\X".to_owned())]
);
assert!(FormatString::parse("\\").is_err());
}

Expand Down Expand Up @@ -798,7 +809,9 @@ mod tests {
follow_links: false
}),
unaligned_directive(FormatDirective::Type { follow_links: true }),
FormatComponent::Literal("?".to_owned()),
// An unrecognized directive is a warning, not a silent drop of
// the '%': it's printed literally, matching GNU find.
FormatComponent::Literal("%?".to_owned()),
]
);

Expand Down
18 changes: 18 additions & 0 deletions tests/test_find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,24 @@ fn find_printf_octal_escape_before_multibyte_char() {
.stdout_only("\0€\n");
}

#[test]
fn find_printf_unrecognized_directive_prints_literally() {
ucmd()
.args(&["./test_data/simple", "-maxdepth", "0", "-printf", "%€|\n"])
.succeeds()
.stdout_contains("%€|\n")
.stderr_contains("unrecognized format directive '%€'");
}

#[test]
fn find_printf_unrecognized_escape_prints_literally() {
ucmd()
.args(&["./test_data/simple", "-maxdepth", "0", "-printf", "\\€|\n"])
.succeeds()
.stdout_contains("\\€|\n")
.stderr_contains("unrecognized escape '\\€'");
}

#[test]
fn find_printf_width_too_large() {
ucmd()
Expand Down
Loading