[git commit] printf: fix handling of omitted arguments
Denys Vlasenko
vda.linux at googlemail.com
Sun Oct 4 15:48:06 UTC 2026
commit: https://git.busybox.net/busybox/commit/?id=f96d33d28a1f70fda5f27d221d5012b1ac0b7dad
branch: https://git.busybox.net/busybox/log/?h=master
For numeric formats, this is meant to behave as a "0" literal zero
argument, not an "" empty string argument.
Add a test to ensure that an explicit "" empty string argument still is
treated as an error.
function old new delta
printf_main 837 1209 +372
print_direc 432 - -432
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 1/0 up/down: 372/-432) Total: -60 bytes
Signed-off-by: Harald van Dijk <harald at gigawatt.nl>
Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
coreutils/printf.c | 14 +++++++-------
testsuite/printf.tests | 10 ++++++++--
2 files changed, 15 insertions(+), 9 deletions(-)
diff --git a/coreutils/printf.c b/coreutils/printf.c
index 3cd48cfcc..341f5093e 100644
--- a/coreutils/printf.c
+++ b/coreutils/printf.c
@@ -355,6 +355,7 @@ static char **print_formatted(char *f, char **argv, int *conv_err)
{
static const char format_chars[] ALIGN1 = "diouxXfeEgGcs";
char *p = (char*)strchr(format_chars, *f);
+ const char *arg = "0";
/* needed - try "printf %" without it */
if (p == NULL || *f == '\0') {
bb_error_msg("%s: invalid format", direc_start);
@@ -373,15 +374,14 @@ static char **print_formatted(char *f, char **argv, int *conv_err)
direc_length += 2;
direc_start = p;
} else {
+ if (p - format_chars >= 11) /* %c or %s? */
+ ++arg; /* "" */
p = NULL;
}
- if (*argv) {
- print_direc(direc_start, direc_length, field_width,
- precision, *argv++);
- } else {
- print_direc(direc_start, direc_length, field_width,
- precision, "");
- }
+ if (*argv)
+ arg = *argv++;
+ print_direc(direc_start, direc_length, field_width,
+ precision, arg);
*conv_err |= errno;
free(p);
}
diff --git a/testsuite/printf.tests b/testsuite/printf.tests
index 728bbf4bf..cdedf5e80 100755
--- a/testsuite/printf.tests
+++ b/testsuite/printf.tests
@@ -116,6 +116,11 @@ testing "printf handles positive numbers for %f" \
"0.420\n""0\n" \
"" ""
+testing "printf accepts missing arguments as empty or 0" \
+ "${bb}printf '%d %i %o %u %x %X %f %e %E %g %G %c %b %s' | ${bb}cat -v" \
+ "0 0 0 0 0 0 0.000000 0.000000e+00 0.000000E+00 0 0 ^@ " \
+ "" ""
+
# "FIXED" now to act compatibly
## We are "more correct" here than bash/coreutils: they happily print -2
@@ -126,11 +131,12 @@ testing "printf handles positive numbers for %f" \
# "" ""
testing "printf handles %d bad_input" \
- "${bb}printf '%d\n' 1 - 2 bad 3 123bad 4 2>&1; echo \$?" \
+ "${bb}printf '%d\n' 1 - 2 bad 3 123bad 4 '' 5 2>&1; echo \$?" \
"1\n""printf: invalid number '-'\n""0\n"\
"2\n""printf: invalid number 'bad'\n""0\n"\
"3\n""printf: invalid number '123bad'\n""0\n"\
-"4\n""1\n" \
+"4\n""printf: invalid number ''\n""0\n"\
+"5\n""1\n" \
"" ""
testing "printf aborts on bare %" \
More information about the busybox-cvs
mailing list