fix: prevent completions from mutating os.Args via append side effect (#2356)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fixes #2257 When getCompletions() checks for interspersed flags, it calls append(finalArgs, "--") to temporarily add a "--" sentinel. However, finalArgs is a sub-slice of the original args (ultimately derived from os.Args[1:] or SetArgs), and if the sub-slice has spare capacity in its backing array, append writes "--" into the shared array, corrupting the caller's data. This is particularly visible when TraverseChildren is enabled: the Traverse method returns sub-slices that share the original backing array. A user's ValidArgsFunction inspecting os.Args would then see "--" where a real argument should be. Copy trimmedArgs at the top of getCompletions to prevent any future append from accidentally mutating the caller's backing array.
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Diff stats
2 files+59 added−1 removed
- other:+59 added−1 removed