SIGN IN SIGN UP
spf13 / cobra UNCLAIMED

A Commander for modern Go CLI interactions

0 0 138 Go

fix: prevent completions from mutating os.Args via append side effect (#2356)

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.

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
V
Varun Chawla committed
746ef07158728502482cea9f880a6f4b21ef29a9
Parent: f2878ba
Committed by GitHub <noreply@github.com> on 4/24/2026, 8:41:57 PM