goccy / go-yaml

YAML support for the Go language
MIT License
1.12k stars 129 forks source link

segfault when using the return of a path which combines IndexAll and Child #436

Open cezarguimaraes opened 5 months ago

cezarguimaraes commented 5 months ago

Describe the bug Attempting to use the filtered value (i.e node.String() or ast.Walk) of a path which combines IndexAll and Child might lead to seg fault if not all sequence elements contain the property being selected by Child

To Reproduce

https://go.dev/play/p/hxD0l_fdVYO

I have added a test case here that reproduces the issue

https://github.com/cezarguimaraes/go-yaml/commit/19b75089e16378395b40a0f17a75c1b406b8334c

Expected behavior

Taking jq as inspiration, partial results should be returned, possibly with nil elements:

$ echo '[{"foo": 1}, {}]' | jq ".[].foo"
1
null

Screenshots image

Version Variables

Additional context I have a tentative fix in this branch https://github.com/goccy/go-yaml/compare/master...cezarguimaraes:go-yaml:fix-indexAll-child-segfault As can be seen by the tests added, the code from this branch simulates jq behavior by returning ast.Null. Another possible approach (preserved by the comments in (*indexAllNode).filter completely filter out unmatched nodes.