Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit f90ceb4

Browse files
committed
Fix check for non-map alias merging in v2 (#529)
The problem does not affect v3.
1 parent 970885f commit f90ceb4

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

decode.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -788,8 +788,7 @@ func (d *decoder) merge(n *node, out reflect.Value) {
788788
case mappingNode:
789789
d.unmarshal(n, out)
790790
case aliasNode:
791-
an, ok := d.doc.anchors[n.value]
792-
if ok && an.kind != mappingNode {
791+
if n.alias != nil && n.alias.kind != mappingNode {
793792
failWantMap()
794793
}
795794
d.unmarshal(n, out)
@@ -798,8 +797,7 @@ func (d *decoder) merge(n *node, out reflect.Value) {
798797
for i := len(n.children) - 1; i >= 0; i-- {
799798
ni := n.children[i]
800799
if ni.kind == aliasNode {
801-
an, ok := d.doc.anchors[ni.value]
802-
if ok && an.kind != mappingNode {
800+
if ni.alias != nil && ni.alias.kind != mappingNode {
803801
failWantMap()
804802
}
805803
} else if ni.kind != mappingNode {

decode_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,7 @@ var unmarshalErrorTests = []struct {
848848
{"a:\n- b: *,", "yaml: line 2: did not find expected alphabetic or numeric character"},
849849
{"a: *b\n", "yaml: unknown anchor 'b' referenced"},
850850
{"a: &a\n b: *a\n", "yaml: anchor 'a' value contains itself"},
851+
{"a: &x null\n<<:\n- *x\nb: &x {}\n", `yaml: map merge requires map or sequence of maps as the value`}, // Issue #529.
851852
{"value: -", "yaml: block sequence entries are not allowed in this context"},
852853
{"a: !!binary ==", "yaml: !!binary value contains invalid base64 data"},
853854
{"{[.]}", `yaml: invalid map key: \[\]interface \{\}\{"\."\}`},
@@ -874,6 +875,13 @@ func (s *S) TestUnmarshalErrors(c *C) {
874875
var value interface{}
875876
err := yaml.Unmarshal([]byte(item.data), &value)
876877
c.Assert(err, ErrorMatches, item.error, Commentf("Partial unmarshal: %#v", value))
878+
879+
if strings.Contains(item.data, ":") {
880+
// Repeat test with typed value.
881+
var value map[string]interface{}
882+
err := yaml.Unmarshal([]byte(item.data), &value)
883+
c.Assert(err, ErrorMatches, item.error, Commentf("Partial unmarshal: %#v", value))
884+
}
877885
}
878886
}
879887

0 commit comments

Comments
 (0)