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

Check for invalid merge #536

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions decode.go
Expand Up @@ -660,7 +660,13 @@ func (d *decoder) mapping(n *node, out reflect.Value) (good bool) {
l := len(n.children)
for i := 0; i < l; i += 2 {
if isMerge(n.children[i]) {
if i+1 >= l {
failf("line %d, column %d: invalid merge, missing value", n.children[i].line+1, n.children[i].column)
}
d.merge(n.children[i+1], out)
if out.IsNil() {
failf("line %d, column %d: invalid null merge", n.children[i+1].line+1, n.children[i+1].column)
}
continue
}
k := reflect.New(kt).Elem()
Expand All @@ -672,6 +678,9 @@ func (d *decoder) mapping(n *node, out reflect.Value) (good bool) {
if kkind == reflect.Map || kkind == reflect.Slice {
failf("invalid map key: %#v", k.Interface())
}
if i+1 >= l {
failf("line %d, column %d: invalid mapping, missing value", n.children[i].line+1, n.children[i].column)
}
e := reflect.New(et).Elem()
if d.unmarshal(n.children[i+1], e) {
d.setMapIndex(n.children[i+1], out, k, e)
Expand Down
33 changes: 33 additions & 0 deletions decode_test.go
Expand Up @@ -1316,6 +1316,39 @@ func (s *S) TestFuzzCrashers(c *C) {
}
}

func (s *S) TestMergeErrors(c *C) {
cases := []struct {
name string
data string
err string
}{
// Issue #529
{
name: `nil merge`,
data: `
a: &8
<<:
- *8

8:
( :
&8
*8:
<<:
- *8
`,
err: "yaml: line 4, column 1: invalid null merge",
},
}

for i, item := range cases {
v := map[string]interface{}{}
err := yaml.Unmarshal([]byte(item.data), &v)
c.Logf("test %d: %s", i, item.name)
c.Assert(err, ErrorMatches, item.err)
}
}

//var data []byte
//func init() {
// var err error
Expand Down