Skip to content

Commit

Permalink
Merge pull request #6032 from RenjiSann/feature/printf-0c-error
Browse files Browse the repository at this point in the history
printf: '%0c' and '%0s' should fail
  • Loading branch information
cakebaker authored Mar 1, 2024
2 parents 75b2bfb + 7094ff1 commit 41f809d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/uucore/src/lib/features/format/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl Spec {
Ok(match type_spec {
// GNU accepts minus, plus and space even though they are not used
b'c' => {
if flags.hash || precision.is_some() {
if flags.zero || flags.hash || precision.is_some() {
return Err(&start[..index]);
}
Self::Char {
Expand All @@ -180,7 +180,7 @@ impl Spec {
}
}
b's' => {
if flags.hash {
if flags.zero || flags.hash {
return Err(&start[..index]);
}
Self::String {
Expand Down
12 changes: 12 additions & 0 deletions tests/by-util/test_printf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,3 +765,15 @@ fn pad_string() {
.stdout_only(expected);
}
}

#[test]
fn format_spec_zero_char_fails() {
// It is invalid to have the format spec '%0c'
new_ucmd!().args(&["%0c", "3"]).fails().code_is(1);
}

#[test]
fn format_spec_zero_string_fails() {
// It is invalid to have the format spec '%0s'
new_ucmd!().args(&["%0s", "3"]).fails().code_is(1);
}

0 comments on commit 41f809d

Please sign in to comment.