fix(printf): bash option parsing, usage errors, early -v validation - #685
Merged
Conversation
printf accepted anything and returned 0 (issue #682). Match bash's internal_getopt("v:") and vflag block: - a missing format operand prints the bare usage line, EX_USAGE (2) - an unknown option is `-x: invalid option' plus usage, EX_USAGE - -v takes its argument attached (-vNAME) or separate; missing is `-v: option requires an argument' plus usage; `printf -v --' takes `--' as the argument (and fails validation), while a lone `-' is the format string - the -v target is validated BEFORE any format processing: a plain identifier or IDENTIFIER[NON-EMPTY-SUBSCRIPT] with the closing bracket last; `a[', `a[]', `1a[0]', `a b' all report `not a valid identifier' with EX_USAGE, and conversions never run Verified byte-identical with bash 5.3.15 on 21 cases; printf.tests scoreboard 36 -> 31, builtins/errors/varenv hold at 0. Closes #682
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 #682. printf accepted any option-looking word and returned 0; it now matches bash's
internal_getopt(\"v:\")and vflag block:printf: -x: invalid option+ usage, exit 2-vargument attached (-vNAME) or separate; missing →-v: option requires an argument+ usage;printf -v --takes--as the option-argument (then fails validation) while a lone-is the format string-vtarget is validated before any format processing: plain identifier orIDENTIFIER[non-empty-subscript]ending in];a[,a[],1a[0],a b,invalid-varall reportnot a valid identifier, exit 2, and conversions never run (printf -v invalid-var '%d' GNUemits only the identifier error)Verification
Byte-identical vs reference bash 5.3.15 on 21 cases (all forms above plus
printf -- -v,printf - hi, valida[0]element targets, and empty-format no-ops). printf.tests scoreboard 36 → 31; builtins/errors/varenv hold at 0; smoke passes.Closes #682
🤖 Generated with Claude Code