From bd18f340f0d808cffc32b5c886b07c73f9b04285 Mon Sep 17 00:00:00 2001 From: Thibault Normand Date: Tue, 10 May 2022 17:56:50 +0200 Subject: [PATCH] fix(decoder): panic raised on fuzzer inputs. Handle yaml_NO_EVENT event type to raise a parsing error vs panic. ref #833 Signed-off-by: Thibault Normand --- decode.go | 3 +++ decode_test.go | 28 ++++++++++++++++------------ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/decode.go b/decode.go index df36e3a3..8e677569 100644 --- a/decode.go +++ b/decode.go @@ -159,6 +159,9 @@ func (p *parser) parse() *Node { return nil case yaml_TAIL_COMMENT_EVENT: panic("internal error: unexpected tail comment event (please report)") + case yaml_NO_EVENT: + failf("unable to build a node from none event") + return nil default: panic("internal error: attempted to parse unknown event (please report): " + p.event.typ.String()) } diff --git a/decode_test.go b/decode_test.go index 5ade5500..a327b094 100644 --- a/decode_test.go +++ b/decode_test.go @@ -767,7 +767,7 @@ var unmarshalTests = []struct { M{"a": 123456e1}, }, { "a: 123456E1\n", - M{"a": 123456E1}, + M{"a": 123456e1}, }, // yaml-test-suite 3GZX: Spec Example 7.1. Alias Nodes { @@ -802,7 +802,6 @@ var unmarshalTests = []struct { "c": []interface{}{"d", "e"}, }, }, - } type M map[string]interface{} @@ -934,6 +933,8 @@ func (s *S) TestUnmarshalDurationInt(c *C) { var unmarshalErrorTests = []struct { data, error string }{ + {"! !00 \xf6", "yaml: unable to build a node from none event"}, + {"! !!0 \xf7", "yaml: unable to build a node from none event"}, {"v: !!float 'error'", "yaml: cannot decode !!str `error` as a !!float"}, {"v: [A,", "yaml: line 1: did not find expected node content"}, {"v:\n- [A,", "yaml: line 2: did not find expected node content"}, @@ -950,14 +951,14 @@ var unmarshalErrorTests = []struct { {"a: 1\nb: 2\nc 2\nd: 3\n", "^yaml: line 3: could not find expected ':'$"}, { "a: &a [00,00,00,00,00,00,00,00,00]\n" + - "b: &b [*a,*a,*a,*a,*a,*a,*a,*a,*a]\n" + - "c: &c [*b,*b,*b,*b,*b,*b,*b,*b,*b]\n" + - "d: &d [*c,*c,*c,*c,*c,*c,*c,*c,*c]\n" + - "e: &e [*d,*d,*d,*d,*d,*d,*d,*d,*d]\n" + - "f: &f [*e,*e,*e,*e,*e,*e,*e,*e,*e]\n" + - "g: &g [*f,*f,*f,*f,*f,*f,*f,*f,*f]\n" + - "h: &h [*g,*g,*g,*g,*g,*g,*g,*g,*g]\n" + - "i: &i [*h,*h,*h,*h,*h,*h,*h,*h,*h]\n", + "b: &b [*a,*a,*a,*a,*a,*a,*a,*a,*a]\n" + + "c: &c [*b,*b,*b,*b,*b,*b,*b,*b,*b]\n" + + "d: &d [*c,*c,*c,*c,*c,*c,*c,*c,*c]\n" + + "e: &e [*d,*d,*d,*d,*d,*d,*d,*d,*d]\n" + + "f: &f [*e,*e,*e,*e,*e,*e,*e,*e,*e]\n" + + "g: &g [*f,*f,*f,*f,*f,*f,*f,*f,*f]\n" + + "h: &h [*g,*g,*g,*g,*g,*g,*g,*g,*g]\n" + + "i: &i [*h,*h,*h,*h,*h,*h,*h,*h,*h]\n", "yaml: document contains excessive aliasing", }, } @@ -1436,7 +1437,10 @@ func (s *S) TestMergeStruct(c *C) { } } -var unmarshalNullTests = []struct{ input string; pristine, expected func() interface{} }{{ +var unmarshalNullTests = []struct { + input string + pristine, expected func() interface{} +}{{ "null", func() interface{} { var v interface{}; v = "v"; return &v }, func() interface{} { var v interface{}; v = nil; return &v }, @@ -1487,7 +1491,7 @@ func (s *S) TestUnmarshalNull(c *C) { func (s *S) TestUnmarshalPreservesData(c *C) { var v struct { A, B int - C int `yaml:"-"` + C int `yaml:"-"` } v.A = 42 v.C = 88