From 70f64e5a4377fe4a216975af179f11cf7e12cf2f Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Thu, 4 Dec 2025 16:04:21 -0600 Subject: [PATCH] Fix missing error check for short patterns, and add tests. Reported by Anton Kling. --- tests/printf.test | 5 +++++ toys/posix/printf.c | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/printf.test b/tests/printf.test index 57f2a283..3d37e1b7 100755 --- a/tests/printf.test +++ b/tests/printf.test @@ -87,3 +87,8 @@ testcmd "%b \e" "'%b' '\\e' | xxd -p" "1b\n" "" "" testcmd "\e" "'\\e' | xxd -p" "1b\n" "" "" testcmd '\0 in %b' '%b ab\\x07\\0x07 | xxd -p' '61620700783037\n' '' '' + +testcmd 'short err1' '% 2>/dev/null || echo yes' 'yes\n' '' '' +testcmd 'short err2' '"%*" 2>/dev/null || echo yes' 'yes\n' '' '' +testcmd 'short err3' '"%*." 2>/dev/null || echo yes' 'yes\n' "" "" +testcmd 'short err4' '"%*.123" 2>/dev/null || echo yes' 'yes\n' "" "" diff --git a/toys/posix/printf.c b/toys/posix/printf.c index c80e6fe4..dbbb36c6 100644 --- a/toys/posix/printf.c +++ b/toys/posix/printf.c @@ -97,7 +97,7 @@ void printf_main(void) // Parse width.precision between % and type indicator. *to++ = '%'; - while (strchr("-+# '0", *f) && (to-toybuf)<10) *to++ = *f++; + while (*f && strchr("-+# '0", *f) && (to-toybuf)<10) *to++ = *f++; for (;;) { if (chrstart(&f, '*')) { if (*arg) wp[i] = atolx(*arg++); @@ -111,6 +111,7 @@ void printf_main(void) aa = *arg ? *arg++ : ""; // Output %esc using parsed format string + if (!c) error_exit("bad %s", *toys.optargs); if (c == 'b') { while (*aa) putchar(chrstart(&aa, '\\') ? handle_slash(&aa, 1) : *aa++); -- 2.39.5