fix(printf): validate numeric conversion arguments like bash - #683
Merged
Conversation
The %d/%i, %o/%u/%x/%X and float branches parsed their argument with a bare strtol/strtod and printed whatever came back, silently accepting `printf '%d' GNU' (issue #680). Match bash's getintmax/getuintmax/ getfloatmax: - a partially or wholly unconverted argument diagnoses `S: invalid number' but still prints the converted prefix (`12abc' is 12, `GNU' and `\'\'' are 0), status 1, processing continues (POSIX) - a well-formed value out of range diagnoses `S: Result too large' (strerror(ERANGE)) and prints the clamped value - a MISSING argument is a silent 0 -- only a present-but-empty string diagnoses - signed conversions now parse and format through long long (bash uses intmax_t), unsigned through unsigned long long, so `%u' takes the full uintmax range and `-1' wraps with no error - float conversions gain the leading '/" character-code form (`%.2f "'s"' is 115.00) Verified byte-identical with bash 5.3.15 on 26 cases; printf.tests scoreboard 66 -> 49, builtins/errors/varenv/arith hold at 0. Closes #680
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #680. The
%d/%i,%o/%u/%x/%Xand float branches parsed arguments with barestrtol/strtodand ignored errors, silently acceptingprintf '%d' GNU. They now match bash'sgetintmax/getuintmax/getfloatmax:S: invalid number, prints the converted prefix (12abc→ 12,GNU/''→ 0), exit 1, processing continues (POSIX)S: Result too large(strerror(ERANGE)), prints the clamped valuelong long(bash uses intmax_t), unsigned throughunsigned long long(%utakes the full uintmax range;-1wraps silently)'/"character-code form (printf '%.2f' "'s"→115.00)Verification
Byte-identical vs reference bash 5.3.15 on 26 cases (invalid/partial/empty/missing args, ±intmax overflow, uintmax wrap, hex/octal self-detection, char codes, float ERANGE→inf). printf.tests scoreboard 66 → 49; builtins/errors/varenv/arith hold at 0; smoke passes.
Closes #680
🤖 Generated with Claude Code