Skip to content

Commit

Permalink
Fix check for non-map alias merging in v2 (#529)
Browse files Browse the repository at this point in the history
The problem does not affect v3.
  • Loading branch information
niemeyer committed Nov 4, 2019
1 parent 970885f commit f90ceb4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
6 changes: 2 additions & 4 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -788,8 +788,7 @@ func (d *decoder) merge(n *node, out reflect.Value) {
case mappingNode:
d.unmarshal(n, out)
case aliasNode:
an, ok := d.doc.anchors[n.value]
if ok && an.kind != mappingNode {
if n.alias != nil && n.alias.kind != mappingNode {
failWantMap()
}
d.unmarshal(n, out)
Expand All @@ -798,8 +797,7 @@ func (d *decoder) merge(n *node, out reflect.Value) {
for i := len(n.children) - 1; i >= 0; i-- {
ni := n.children[i]
if ni.kind == aliasNode {
an, ok := d.doc.anchors[ni.value]
if ok && an.kind != mappingNode {
if ni.alias != nil && ni.alias.kind != mappingNode {
failWantMap()
}
} else if ni.kind != mappingNode {
Expand Down
8 changes: 8 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,7 @@ var unmarshalErrorTests = []struct {
{"a:\n- b: *,", "yaml: line 2: did not find expected alphabetic or numeric character"},
{"a: *b\n", "yaml: unknown anchor 'b' referenced"},
{"a: &a\n b: *a\n", "yaml: anchor 'a' value contains itself"},
{"a: &x null\n<<:\n- *x\nb: &x {}\n", `yaml: map merge requires map or sequence of maps as the value`}, // Issue #529.
{"value: -", "yaml: block sequence entries are not allowed in this context"},
{"a: !!binary ==", "yaml: !!binary value contains invalid base64 data"},
{"{[.]}", `yaml: invalid map key: \[\]interface \{\}\{"\."\}`},
Expand All @@ -874,6 +875,13 @@ func (s *S) TestUnmarshalErrors(c *C) {
var value interface{}
err := yaml.Unmarshal([]byte(item.data), &value)
c.Assert(err, ErrorMatches, item.error, Commentf("Partial unmarshal: %#v", value))

if strings.Contains(item.data, ":") {
// Repeat test with typed value.
var value map[string]interface{}
err := yaml.Unmarshal([]byte(item.data), &value)
c.Assert(err, ErrorMatches, item.error, Commentf("Partial unmarshal: %#v", value))
}
}
}

Expand Down

0 comments on commit f90ceb4

Please sign in to comment.