Fix index out of range panic when Run is given no arguments - #2401
Open
mrueg wants to merge 1 commit into
Open
Conversation
Command.Run indexes into the argument slice in two places without first
checking that it is non-empty, so passing an empty slice panics rather
than returning:
* setupDefaults reads osArgs[0] to derive a default command name,
panicking with "index out of range [0] with length 0" for any root
command that does not set Name explicitly.
* checkShellCompleteFlag reads arguments[len(arguments)-1], panicking
with "index out of range [-1]" whenever EnableShellCompletion is set.
Guard both. A command with no arguments now runs its Action, and simply
keeps an empty Name when there is no argv[0] to derive one from.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.
Command.Runindexes into the argument slice in two places without first checking that it is non-empty, so passing an empty slice panics instead of returning.setupDefaultsReads
osArgs[0]to derive a default command name:Affects any root command that does not set
Nameexplicitly.checkShellCompleteFlagReads
arguments[len(arguments)-1]:Affects any command with
EnableShellCompletionset, named or not.Fix
Guard both index accesses. A command invoked with no arguments now runs its
Actionas it would with any other argument list, and simply keeps an emptyNamewhen there is noargv[0]to derive one from.This is a pure robustness fix: in practice callers pass
os.Args, which is never empty, so no working behaviour changes — the only reachable states previously were the two panics. It matters mostly for tests and for library consumers that build an argument slice programmatically.Testing
Adds
TestRunWithNoOsArgs(named / shell-completion-enabled / unnamed) and ano arguments at allcase to the existingTest_checkShellCompleteFlagtable. Both fail onmainwithout the change.🤖 Generated with Claude Code