From 58ee0ce42739b7762b31cc876eeb57694940f71a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dorian=20P=C3=A9ron?= Date: Fri, 1 Mar 2024 00:02:31 +0100 Subject: [PATCH 1/2] tests/printf: Verify the correct error behavior of printf when provided with '%0c' or '%0s' --- tests/by-util/test_printf.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/by-util/test_printf.rs b/tests/by-util/test_printf.rs index 38d7b10a6..0cb603da4 100644 --- a/tests/by-util/test_printf.rs +++ b/tests/by-util/test_printf.rs @@ -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); +} From 7094ff17cfc772f12b21c527bdb39cf5fd6e8d7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dorian=20P=C3=A9ron?= Date: Fri, 1 Mar 2024 00:15:58 +0100 Subject: [PATCH 2/2] printf: Raise error on '%0c' and '%0s' formats --- src/uucore/src/lib/features/format/spec.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/uucore/src/lib/features/format/spec.rs b/src/uucore/src/lib/features/format/spec.rs index af8a912d2..7c173a3a9 100644 --- a/src/uucore/src/lib/features/format/spec.rs +++ b/src/uucore/src/lib/features/format/spec.rs @@ -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 { @@ -180,7 +180,7 @@ impl Spec { } } b's' => { - if flags.hash { + if flags.zero || flags.hash { return Err(&start[..index]); } Self::String {