Skip to content

Port #817 from v2.9.4 to master - #818

Merged
fxamacker merged 2 commits into
masterfrom
fxamacker/fix-hashable-map-key
Sep 15, 2026
Merged

fxamacker merged 2 commits into
masterfrom
fxamacker/fix-hashable-map-key

Conversation

@fxamacker

Copy link
Copy Markdown
Owner

See v2.9.4 release notes and PR #817 for details.

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 d1789e8 into master Sep 15, 2026
16 checks passed
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