Fix missing error check for short patterns, and add tests.

Reported by Anton Kling.
This commit is contained in:
Rob Landley 2025-12-04 16:04:21 -06:00
parent b72880d6dc
commit 70f64e5a43
2 changed files with 7 additions and 1 deletions

View File

@ -87,3 +87,8 @@ testcmd "%b \e" "'%b' '\\e' | xxd -p" "1b\n" "" ""
testcmd "\e" "'\\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 '\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' "" ""

View File

@ -97,7 +97,7 @@ void printf_main(void)
// Parse width.precision between % and type indicator. // Parse width.precision between % and type indicator.
*to++ = '%'; *to++ = '%';
while (strchr("-+# '0", *f) && (to-toybuf)<10) *to++ = *f++; while (*f && strchr("-+# '0", *f) && (to-toybuf)<10) *to++ = *f++;
for (;;) { for (;;) {
if (chrstart(&f, '*')) { if (chrstart(&f, '*')) {
if (*arg) wp[i] = atolx(*arg++); if (*arg) wp[i] = atolx(*arg++);
@ -111,6 +111,7 @@ void printf_main(void)
aa = *arg ? *arg++ : ""; aa = *arg ? *arg++ : "";
// Output %esc using parsed format string // Output %esc using parsed format string
if (!c) error_exit("bad %s", *toys.optargs);
if (c == 'b') { if (c == 'b') {
while (*aa) while (*aa)
putchar(chrstart(&aa, '\\') ? handle_slash(&aa, 1) : *aa++); putchar(chrstart(&aa, '\\') ? handle_slash(&aa, 1) : *aa++);