Skip to content

Panics from fuzzing #540

@bep

Description

@bep

This fuzz test could possibly be a starting point for #466:

package yaml_test

import (
	"strings"
	"testing"

	"github.com/goccy/go-yaml"
)

func FuzzUnmarshalToMap(f *testing.F) {
	const validYAML = `
id: 1
message: Hello, World
verified: true
`

	invalidYAML := []string{
		"0::",
		"{0",
		"*-0",
		">\n>",
		"&{0",
		"0_",
		"0\n:",
		"0\n-",
		"0\n0",
		"0\n0\n",
		"0\n0\n0",
		"0\n0\n0\n",
		"0\n0\n0\n0",
		"0\n0\n0\n0\n",
		"0\n0\n0\n0\n0",
		"0\n0\n0\n0\n0\n",
		"0\n0\n0\n0\n0\n0",
		"0\n0\n0\n0\n0\n0\n",
		"",
	}

	f.Add([]byte(validYAML))
	for _, s := range invalidYAML {
		f.Add([]byte(s))
		f.Add([]byte(validYAML + s))
		f.Add([]byte(s + validYAML))
		f.Add([]byte(s + validYAML + s))
		f.Add([]byte(strings.Repeat(s, 3)))
	}

	f.Fuzz(func(t *testing.T, src []byte) {
		v := map[string]any{}
		_ = yaml.Unmarshal(src, &v)
	})
}

Some example panics:

---
# reflect.Value.SetMapIndex: value of type bool is not assignable to type string
true :
---
# reflect.Value.SetMapIndex: value of type float64 is not assignable to type string
10.:
---
# runtime error: invalid memory address or nil pointer dereference
{? {? 
---
# slice bounds out of range
|0#��0�����0�������0�������0�����0�����0��������������������0���������������0�����0�������0��0��������00����0�����������0�������������0����0����0���
���0����0����������
�������0���������0����������0�������0����������0����0��0����
������00����
���������0�����0��������0��������������������0����0��0���0�����0�����������������������00���������0���0����
---
# index out of range
>1
 ����
�0
---
# reflect.Value.SetMapIndex: value of type map[string]interface {} is not assignable to type string
{? {jui��l��U4��7�{�K%�dv���(�I�B�F�D����$�����T�E}Yd�|��ѐ2�K��Y��e���ʞ���M��i}#BTx�]���{V[�1 ��m��L�/�? {? {%

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingparser

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions