Skip to content

Fix panic decoding uncomparable map keys under certain conditions - #817

Merged
fxamacker merged 2 commits into
release-2.9from
fxamacker/release-2.9-fix-hashable-map-key
Sep 14, 2026
Merged

fxamacker merged 2 commits into
release-2.9from
fxamacker/release-2.9-fix-hashable-map-key

Conversation

@fxamacker

Copy link
Copy Markdown
Owner

This issue can be encountered when one of these two conditions is met:

  • user application registers a tag type (via TagSet interface) that is an array or struct with uncomparable or interface{} element or field type, or
  • user application specifies a destination Go map whose key type is a named empty interface type or a type containing an interface.

Not 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 to any, 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, and cbor.Tag with 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:

  • the user application registers a tag type of array or struct whose element or field type is uncomparable or interface{}, and
  • the CBOR map contains the registered tag with uncomparable content as a map key, and
  • the destination type is any, map[any]X, or a struct when DupMapKey is DupMapKeyEnforcedAPF.

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.

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.
@fxamacker fxamacker self-assigned this Sep 14, 2026
@fxamacker
fxamacker merged commit be0da0d into release-2.9 Sep 14, 2026
25 checks passed
fxamacker added a commit that referenced this pull request Sep 15, 2026
@fxamacker
fxamacker deleted the fxamacker/release-2.9-fix-hashable-map-key branch October 1, 2026 23:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants