From 3f5ef4cca18d851422b2f9ab1b4d673149525337 Mon Sep 17 00:00:00 2001 From: Reuven Harrison Date: Mon, 26 Oct 2026 17:37:03 +0200 Subject: [PATCH] Report an unhashable merged key as an error, not a panic A mapping being merged has each of its keys looked up among the keys already seen, before the check that rejects a map or a sequence as a key. Hashing one of those panics, so a document like <<: ? [1] : v 1: x took down the caller instead of returning an error. Guard the lookup and the store the way go-yaml does upstream, which turns the panic into an ordinary yaml error. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_017e285CvofYf6joqC98gTXz --- decode.go | 27 ++++++++++++++++++++++++--- decode_test.go | 4 ++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/decode.go b/decode.go index f36d6bf6..82f60ddc 100644 --- a/decode.go +++ b/decode.go @@ -924,10 +924,10 @@ func (d *decoder) mapping(n *Node, out reflect.Value) (good bool) { if d.unmarshal(n.Content[i], k) { if mergedFields != nil { ki := k.Interface() - if mergedFields[ki] { + if d.getPossiblyUnhashableKey(mergedFields, ki) { continue } - mergedFields[ki] = true + d.setPossiblyUnhashableKey(mergedFields, ki, true) } kkind := k.Kind() if kkind == reflect.Interface { @@ -1052,6 +1052,27 @@ func failWantMap() { failf("map merge requires map or sequence of maps as the value") } +// A key of a mapping being merged is not checked for hashability before it is +// looked up, and a map or a sequence key panics when hashed. Report it as a +// yaml error, like the key check further down the mapping does. +func (d *decoder) setPossiblyUnhashableKey(m map[interface{}]bool, key interface{}, value bool) { + defer func() { + if err := recover(); err != nil { + failf("%v", err) + } + }() + m[key] = value +} + +func (d *decoder) getPossiblyUnhashableKey(m map[interface{}]bool, key interface{}) bool { + defer func() { + if err := recover(); err != nil { + failf("%v", err) + } + }() + return m[key] +} + func (d *decoder) merge(parent *Node, merge *Node, out reflect.Value) { mergedFields := d.mergedFields if mergedFields == nil { @@ -1059,7 +1080,7 @@ func (d *decoder) merge(parent *Node, merge *Node, out reflect.Value) { for i := 0; i < len(parent.Content); i += 2 { k := reflect.New(ifaceType).Elem() if d.unmarshal(parent.Content[i], k) { - d.mergedFields[k.Interface()] = true + d.setPossiblyUnhashableKey(d.mergedFields, k.Interface(), true) } } } diff --git a/decode_test.go b/decode_test.go index 94bc0250..8ff73f69 100644 --- a/decode_test.go +++ b/decode_test.go @@ -1036,6 +1036,10 @@ var unmarshalErrorTests = []struct { {"a: !!binary ==", "yaml: !!binary value contains invalid base64 data"}, {"{[.]}", `yaml: invalid map key: \[\]interface \{\}\{"\."\}`}, {"{{.}}", `yaml: invalid map key: map\[string]interface \{\}\{".":interface \{\}\(nil\)\}`}, + // A map or sequence key of a merged mapping is looked up among the keys + // already seen before the key check rejects it, and hashing one panics. + {"<<:\n ? [1]\n : v\n1: x\n", `yaml: runtime error: hash of unhashable type \[\]interface \{\}`}, + {"<<:\n ? {k: v}\n : 1\n1: x\n", `yaml: runtime error: hash of unhashable type map\[string]interface \{\}`}, {"b: *a\na: &a {c: 1}", `yaml: unknown anchor 'a' referenced`}, {"%TAG !%79! tag:yaml.org,2002:\n---\nv: !%79!int '1'", "yaml: did not find expected whitespace"}, {"a:\n 1:\nb\n 2:", ".*could not find expected ':'"},