Return an error instead of panicking on a map with an unmappable key/value type - #627
Open
chuenchen309 wants to merge 1 commit into
Open
Return an error instead of panicking on a map with an unmappable key/value type#627chuenchen309 wants to merge 1 commit into
chuenchen309 wants to merge 1 commit into
Conversation
…value type `mapDecoder` looked up the key and value mappers with `Registry.ForNamedType`, which is documented to return nil when no mapper can be determined, but used the results without a nil check. A map flag whose key or value type has no registered mapper (e.g. `map[string]any`, a very common idiom for arbitrary config, or `map[complex128]string`) therefore nil-panicked at decode time. The sibling `sliceDecoder` already guards this exact case and returns "no mapper for element type of ...". Mirror that for maps so both paths fail the same way with a descriptive error.
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 map flag whose key or value type has no registered mapper panics with a nil-pointer dereference at decode time, instead of returning an error:
map[string]any/map[string]interface{}is a common idiom for "arbitrary key/value config", so this is easy to hit by accident.Root cause (
mapper.go,mapDecoder): the key and value mappers are looked up withRegistry.ForNamedType, whose doc says "Will return nil if a mapper can not be determined", but the results were used without a nil check (keyDecoder.Decode/valueDecoder.Decode).The sibling already handles this.
sliceDecoderguards the identical case and returns a clean error —[]complex128gives--f: no mapper for element type of []complex128, whilemap[string]complex128panics. This just mirrors that guard for maps.Fix: nil-check both decoders and return
no mapper for key type of .../no mapper for value type of ..., matching the slice path.Verification (re-runnable from the diff):
map[string]anyandmap[complex128]stringpanic (nil deref atmapper.go:526);[]complex128already returns a clean error.--set: no mapper for value type of map[string]interface {}/--set: no mapper for key type of map[complex128]string; the slice path is unchanged.TestMapFlagWithSliceValue; they panic on the unfixed tree and pass with the fix.go test ./...is green.Disclosure: This PR was authored by an AI coding agent (Claude Code) running on this account: the AI found the bug, ran the repro, wrote the tests, and wrote this description. The human account holder reviews every change and is accountable for it. The verification above is real and re-runnable from the diff. If this isn't the kind of contribution you want, say so and I'll close it.