Repository navigation
Fix panic decoding uncomparable map keys under certain conditions - #817
Merged
fxamacker merged 2 commits intoSep 14, 2026
Merged
Conversation
This commit fixes a bug that can only be encountered if a user app
registers a tag for an array or struct having uncomparable or interface{}
element type, and then tries to decode a CBOR map matching that
registered tag as a map key under specific conditions (see details below).
It is fixed by checking comparability of array and struct values
before inserting them as a map key.
Details
Although isHashableValue() correctly rejected slice, map, func, big.Int,
and cbor.Tag having uncomparable content, it failed to reject arrays and
structs whose element or field is uncomparable. This can cause a panic
when decoding a CBOR map if all of these conditions are met:
- The application registers a tag type of array or struct whose element or
field type is uncomparable or interface{}.
- CBOR map contains the registered tag with uncomparable content as map key.
- Destination type is an interface{}, map[interface{}]X, or a struct when
DupMapKey is DupMapKeyEnforcedAPF.
This commit fixes a bug that can only be encountered if a user app specifies a decoding destination Go map key that is a user-defined empty interface or a type with transitive interface, and then tries to decode a CBOR map with an uncomparable map key value into it. This is fixed by checking map key comparability for key type that is interface or contains transitive interface before inserting them as a map key. Details Previously, the map key comparability is only checked when Go map key type is "any", so uncomparable value can cause panic when inserting as map key if: - the destination map key type is a user-defined empty interface, or - the destination map key type is a comparable type transitively containing an interface (e.g. [1]any). The commit updates the map key comparability check gate from whether the map key type is "any" to whether the map key type's comparability can't be determined from the static type alone.
x448
approved these changes
Sep 14, 2026
fxamacker
added a commit
that referenced
this pull request
Sep 15, 2026
Port #817 from v2.9.4 to master
3 of 5 tasks
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.
This issue can be encountered when one of these two conditions is met:
TagSetinterface) that is an array or struct with uncomparable orinterface{}element or field type, orNot affected: user apps that don't register tag types and don't use map key types that either are named empty interfaces (e.g.
type MyAny any) or contain interfaces.This PR fixes these panics by extending the existing map key comparability check:
isHashableValue()now also rejects arrays and structs containing uncomparable values, and the check now runs for every map key type whose comparability can't be determined from the static type alone (in addition toany, which was already checked).This was discovered while improving tests and reviewing code for an upcoming release.
Tests and fuzz tests were also updated to detect this.
Details
Registered tag type with uncomparable content as a map key
Although
isHashableValue()correctly rejected slice, map, func,big.Int, andcbor.Tagwith uncomparable content, it didn't reject arrays and structs whose element or field is uncomparable. This can cause a panic when decoding a CBOR map if ALL of these conditions are met:interface{}, andany,map[any]X, or a struct whenDupMapKeyisDupMapKeyEnforcedAPF.Named empty interface or a type containing an interface as a map key
Previously, map key comparability was only checked when the Go map key type was
any, so decoding a CBOR map with an uncomparable key could cause a panic if the destination map key type is a named empty interface, or a comparable type transitively containing an interface.