Repository navigation
fix(encoding): avoid nil panic when encoding unknown enum number - #3904
Open
amalsaidov wants to merge 1 commit into
Open
amalsaidov wants to merge 1 commit into
amalsaidov wants to merge 1 commit into
Conversation
EncodeField looked up the enum value descriptor by number and used its name without checking whether the lookup succeeded. proto3 enums are open, so a field can legally hold a number that has no declared value, for example one produced by a newer peer. In that case ByNumber returns nil and string(desc.Name()) dereferences a nil pointer, crashing the whole process when a response or request carrying such a value is encoded into form/query values. Fall back to the numeric value when the descriptor is not found, which matches how protojson renders unknown enum numbers and what the form decoder already accepts. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This branch has not been deployed
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.
Description (what this PR does / why we need it):
EncodeFieldinencoding/formresolves an enum value by its number and immediately uses the returned descriptor's name:proto3 enums are open: a field can legally hold a number that has no declared value, for example a value written by a newer peer that defines more enum entries. When that happens
ByNumberreturnsnil, anddesc.Name()dereferences a nil pointer. Encoding such a message into form/query values (any HTTP request/response that goes through the form codec with an unknown enum number) panics and takes down the process.This PR checks the lookup result and falls back to the numeric value when the descriptor is not found. That matches how
protojsonrenders unknown enum numbers, and the form decoder already accepts an enum given as a number, so the value round-trips for known numbers.Before the change the added test panics with:
Which issue(s) this PR fixes (resolves / be part of):
None filed; found while reading the form codec.
Other special notes for the reviewers:
TestEncodeUnknownEnum, which sets aSexfield to a number with no declared name and asserts the encoded value is the number.go build ./...,go test ./encoding/...,gofmt -landgo vet ./encoding/form/are clean.🤖 Generated with Claude Code