Fix nil pointer dereference for interface-typed flag values - #2400
Open
mrueg wants to merge 1 commit into
Open
Conversation
reflect.TypeOf returns nil when passed a nil interface value. FlagBase
is generic over the value type T, and for GenericFlag that type is the
Value interface, so reflect.TypeOf(f.Value) and reflect.TypeOf(zero)
both yield nil there. Two call sites dereferenced the result without
checking, panicking on an otherwise valid flag:
* SchemaItemsType() panics for any GenericFlag, since it always
inspects reflect.TypeOf(zero).Kind().
* PostParse() panics for a GenericFlag whose value source resolves to
an empty string. The `val != ""` short circuit hid this whenever the
source produced a non-empty value, so it only reproduces with an
empty environment variable or file.
TypeName() and IsMultiValueFlag() already guard against a nil type; do
the same in both of these. PostParse() now derives the kind once and
reuses it, which also drops a duplicate reflect.TypeOf call.
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.
reflect.TypeOfreturnsnilwhen passed a nil interface value.FlagBaseis generic over its value typeT, and forGenericFlagthat type is theValueinterface, so bothreflect.TypeOf(f.Value)andreflect.TypeOf(zero)yieldnilthere. Two call sites dereference the result without checking, panicking on an otherwise perfectly valid flag.SchemaItemsType()Panics for any
GenericFlag, since it unconditionally callsreflect.TypeOf(zero).Kind():PostParse()Panics for a
GenericFlagwhose value source resolves to an empty string:The
val != ""short circuit meansreflect.TypeOf(f.Value).Kind()is never evaluated when the source produces a non-empty value, so this only reproduces with an empty environment variable or an empty file. That is why it has gone unnoticed.Fix
TypeName()andIsMultiValueFlag()already guard against a nil type; this does the same in both of the unguarded spots.PostParse()now derives the kind once up front and reuses it, which also removes a duplicatereflect.TypeOfcall.No public API change —
go doc -alloutput is identical totestdata/godoc-v3.x.txt.Testing
Adds
TestFlagBaseInterfaceValueTypecoveringSchemaItemsType,PostParsewith an empty source value, andPostParsewith a non-empty source value (to pin the non-panicking path). Both subtests panic onmainwithout the change.🤖 Generated with Claude Code