Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle unhashable keys during merge #122

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions goyaml.v2/patch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package yaml_test

import (
"testing"

yaml "sigs.k8s.io/yaml/goyaml.v2"
)

func TestIssue117(t *testing.T) {
data := []byte(`
a:
<<:
-
?
-
`)

x := map[string]interface{}{}
err := yaml.Unmarshal([]byte(data), &x)
if err == nil {
t.Errorf("expected error, got none")
}
}
24 changes: 21 additions & 3 deletions goyaml.v3/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -832,10 +832,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 {
Expand Down Expand Up @@ -956,14 +956,32 @@ func failWantMap() {
failf("map merge requires map or sequence of maps as the value")
}

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 {
d.mergedFields = make(map[interface{}]bool)
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)
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions goyaml.v3/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package yaml_test

import (
"bytes"
"testing"

. "gopkg.in/check.v1"
yaml "sigs.k8s.io/yaml/goyaml.v3"
Expand Down Expand Up @@ -158,3 +159,19 @@ func (s *S) TestNewLinePreserved(c *C) {
// the newline at the start of the file should be preserved
c.Assert(string(data), Equals, "_: |4\n\n a:\n b:\n c: d\n")
}

func TestIssue117(t *testing.T) {
data := []byte(`
a:
<<:
-
?
-
`)

x := map[string]interface{}{}
err := yaml.Unmarshal([]byte(data), &x)
if err == nil {
t.Errorf("expected error, got none")
}
}
Loading