← Commit list

f96d33d28

Harald van Dijk
Harald van Dijk
2026-10-04
SUCCESS Duration: 241s

Subject

printf: fix handling of omitted arguments

Binary

No binary (build failed)

Architecture & Compiler Flags

Architecture: x86_64-linux-gnu Compiler: gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0

—

Build Log

(log yok)

Commit Diff GitHub →

coreutils/printf.c+7 -7
@@ -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);
}
testsuite/printf.tests+8 -2
@@ -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 %" \