Fix panic on a set tag without a value - #623
Open
chuenchen309 wants to merge 1 commit into
Open
Conversation
The guard contradicts its own next line:
parts := strings.SplitN(set, "=", 2)
if len(parts) == 0 {
return fmt.Errorf("set should be in the form key=value but got %q", set)
}
t.Vars[parts[0]] = parts[1]
SplitN never returns a zero-length slice, so the check is dead and parts[1]
indexes a length-1 slice whenever the tag has no "=":
Flag string `set:"novalue"` -> panic: index out of range [1] with length 1
Flag string `short:"ab"` -> error: invalid short flag name "ab": invalid rune
Flag string `set:"k=v"` -> nil
The error message on the line above already says exactly what should happen --
it just could not fire. Another malformed tag on a sibling path returns
cleanly, so this is the odd one out.
README documents the tag as set:"K=V", so a missing "=" is an ordinary typo,
and it reaches this through kong.New / kong.Must / kong.Parse.
Compare against len(parts) != 2 so the intended error is returned.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
alecthomas
approved these changes
Jul 18, 2026
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.
A
settag without a=crashes the program instead of returning the error kong already wrote for it.The guard contradicts its own next line:
strings.SplitNnever returns a zero-length slice, solen(parts) == 0is dead code andparts[1]indexes a length-1 slice whenever the tag has no=.Reproduction
For comparison, another malformed tag on a sibling path returns cleanly, and the well-formed case is unaffected:
The README documents the tag as
set:"K=V", so a missing=is an ordinary typo, and it reaches this throughkong.New/kong.Must/kong.Parse.Fix
Compare against
len(parts) != 2, so the message on the line above can actually fire:Added
TestTagSetWithoutValueErrorsalongsideTestInvalidRuneErrors, which is the same shape.go test ./...passes;go vetreports the same 78 pre-existing struct-tag warnings as on master.Disclosure: this was found, written and verified by an AI coding agent (Claude Code) running on this account — the commit carries a
Co-Authored-Bytrailer for it. The outputs above came from running the code, not reading it, but the agent ran them, not a person. Happy to adjust anything.