From 2b21a445652488099a64067e13b98576f78f0363 Mon Sep 17 00:00:00 2001 From: Andrey Konovalov Date: Mon, 24 Jul 2017 15:05:08 +0200 Subject: prog: return error instead of panic when parsing --- prog/encoding.go | 2 +- prog/encoding_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) (limited to 'prog') diff --git a/prog/encoding.go b/prog/encoding.go index 93af09457..c9d5d40c5 100644 --- a/prog/encoding.go +++ b/prog/encoding.go @@ -216,7 +216,7 @@ func parseArg(typ sys.Type, p *parser, vars map[string]Arg) (Arg, error) { case *sys.VmaType: arg = pointerArg(typ, 0, 0, 0, nil) default: - panic(fmt.Sprintf("bad const type %+v", typ)) + return nil, fmt.Errorf("bad const type %+v", typ) } case 'r': id := p.Ident() diff --git a/prog/encoding_test.go b/prog/encoding_test.go index f23a92cc7..f4036b458 100644 --- a/prog/encoding_test.go +++ b/prog/encoding_test.go @@ -6,6 +6,7 @@ package prog import ( "fmt" "reflect" + "regexp" "sort" "testing" ) @@ -93,3 +94,32 @@ func TestCallSetRandom(t *testing.T) { } } } + +func TestDeserialize(t *testing.T) { + tests := []struct { + data string + err *regexp.Regexp + }{ + { + "syz_test$struct(&(0x7f0000000000)={0x0, {0x0}})", + nil, + }, + { + "syz_test$struct(&(0x7f0000000000)=0x0)", + regexp.MustCompile(`bad const type.*`), + }, + } + for _, test := range tests { + _, err := Deserialize([]byte(test.data)) + if err != nil { + if test.err == nil { + t.Fatalf("deserialization failed with\n%s\ndata:\n%s\n", err, test.data) + } + if !test.err.MatchString(err.Error()) { + t.Fatalf("deserialization failed with\n%s\nwhich doesn't match\n%s\ndata:\n%s\n", err, test.err, test.data) + } + } else if test.err != nil { + t.Fatalf("deserialization should have failed with:\n%s\ndata:\n%s\n", test.err, test.data) + } + } +} -- cgit mrf-deployment