aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2021-12-16 11:19:03 +0000
committerDenys Vlasenko <vda.linux@googlemail.com>2021-12-17 21:13:26 +0100
commit7105e4afddbf47b494accce40e2a701b8833e6ce (patch)
tree2b4cb2ea98fe013a0ca45e218a732bf575efaacd
parentb720629dfec0e8e991e75b751dad215af2bc657f (diff)
downloadbusybox-w32-7105e4afddbf47b494accce40e2a701b8833e6ce.tar.gz
busybox-w32-7105e4afddbf47b494accce40e2a701b8833e6ce.tar.bz2
busybox-w32-7105e4afddbf47b494accce40e2a701b8833e6ce.zip
printf: allow 0 as a flag and allow multiple flags
The '%' character in a format specification may be followed by one or more flags from the list "+- #0". BusyBox printf didn't support the '0' flag or allow multiple flags to be provided. As a result the formats '%0*d' and '%0 d' were considered to be invalid. The lack of support for '0' was pointed out by Andrew Snyder on the musl mailing list: https://www.openwall.com/lists/musl/2021/12/14/2 function old new delta printf_main 860 891 +31 .rodata 99281 99282 +1 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 2/0 up/down: 32/0) Total: 32 bytes Signed-off-by: Ron Yorston <rmy@pobox.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--coreutils/printf.c2
-rwxr-xr-xtestsuite/printf.tests10
2 files changed, 11 insertions, 1 deletions
diff --git a/coreutils/printf.c b/coreutils/printf.c
index dd94c8ade..2e672d15f 100644
--- a/coreutils/printf.c
+++ b/coreutils/printf.c
@@ -313,7 +313,7 @@ static char **print_formatted(char *f, char **argv, int *conv_err)
313 } 313 }
314 break; 314 break;
315 } 315 }
316 if (*f && strchr("-+ #", *f)) { 316 while (*f && strchr("-+ #0", *f)) {
317 ++f; 317 ++f;
318 ++direc_length; 318 ++direc_length;
319 } 319 }
diff --git a/testsuite/printf.tests b/testsuite/printf.tests
index 050edef71..728bbf4bf 100755
--- a/testsuite/printf.tests
+++ b/testsuite/printf.tests
@@ -143,4 +143,14 @@ testing "printf aborts on %r" \
143 "printf: %r: invalid format\n""1\n" \ 143 "printf: %r: invalid format\n""1\n" \
144 "" "" 144 "" ""
145 145
146testing "printf treats leading 0 as flag" \
147 "${bb}printf '%0*d\n' 2 1 2>&1; echo \$?" \
148 "01\n""0\n" \
149 "" ""
150
151testing "printf handles multiple flags" \
152 "${bb}printf '%0 d\n' 2 2>&1; echo \$?" \
153 " 2\n""0\n" \
154 "" ""
155
146exit $FAILCOUNT 156exit $FAILCOUNT