Skip to content

fix(printf): diagnose missing/invalid format characters; support %S/%C - #684

Merged
brianjfox merged 1 commit into
mainfrom
fix/printf-format-char-validation
Aug 20, 2026
Merged

fix(printf): diagnose missing/invalid format characters; support %S/%C#684
brianjfox merged 1 commit into
mainfrom
fix/printf-format-char-validation

Conversation

@brianjfox

Copy link
Copy Markdown
Owner

Summary

Fixes #681. Bad conversion specs were echoed literally with status 0; they now match bash:

  • conversion running off the end of the format (bare %, dangling %5, trailing length modifiers %z/%h/%L/%lz) → %SPEC': missing format character with the whole spec quoted
  • unknown conversion character → C': invalid format character (%y famously infinite-looped ancient printfs)
  • both stop processing with exit 1 while accumulated output is still written (printf 'ab%Mcd' x prints ab)
  • unclosed %( keeps bash's literal-echo behavior
  • %S/%C recognized as bash's synonyms for wide %ls/%lc in multibyte locales (character-counting width/precision), plain %s/%c otherwise

Verification

Byte-identical vs reference bash 5.3.15 on 20 cases, including all dangling-modifier forms, mid-format stop, %S width/precision in UTF-8, and no-regression checks on %%, %ld, %lld, %Lf, %5.2d. printf.tests scoreboard 49 → 36; builtins/errors/varenv/heredoc hold at 0; smoke passes.

Closes #681

🤖 Generated with Claude Code

Bad conversion specs were echoed literally with status 0 (issue #681).
Match bash:

- a conversion that runs off the end of the format -- a bare `%', a
  dangling `%5', or trailing length modifiers like `%z' / `%lz' --
  reports ``%SPEC': missing format character' with the whole spec
- an unknown conversion character reports ``C': invalid format
  character' (`%y' used to infinite-loop old printfs)
- both stop processing with status 1 while the accumulated output is
  still written (`ab%Mcd' prints `ab')
- an unclosed `%(' keeps bash's literal-echo behavior
- %S and %C are recognized as bash's synonyms for the wide %ls / %lc
  in a multibyte locale (character-counting width/precision), plain
  %s / %c otherwise

Verified byte-identical with bash 5.3.15 on 20 cases; printf.tests
scoreboard 49 -> 36, builtins/errors/varenv/heredoc hold at 0.

Closes #681
@brianjfox
brianjfox merged commit de161dd into main Aug 20, 2026
1 check passed
@brianjfox
brianjfox deleted the fix/printf-format-char-validation branch August 20, 2026 07:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

printf: missing/invalid format characters are echoed instead of diagnosed

1 participant