From 64db29d85ff91ba669cfaf009d5f400a4da8a55f Mon Sep 17 00:00:00 2001 From: Stan Nelson Date: Tue, 5 Jun 2018 19:01:39 -0700 Subject: [PATCH] jsonpb: error on scalar enum provided for repeated enums instead of panic --- jsonpb/jsonpb.go | 3 +++ jsonpb/jsonpb_test.go | 1 + 2 files changed, 4 insertions(+) diff --git a/jsonpb/jsonpb.go b/jsonpb/jsonpb.go index 399fa4a69c..242e3155bd 100644 --- a/jsonpb/jsonpb.go +++ b/jsonpb/jsonpb.go @@ -878,6 +878,9 @@ func (u *Unmarshaler) unmarshalValue(target reflect.Value, inputValue json.RawMe target.Set(reflect.New(targetType.Elem())) target = target.Elem() } + if targetType.Kind() != reflect.Int32 { + return fmt.Errorf("invalid target %q for enum %s", targetType.Kind(), prop.Enum) + } target.SetInt(int64(n)) return nil } diff --git a/jsonpb/jsonpb_test.go b/jsonpb/jsonpb_test.go index 8a0833f533..54ed681a03 100644 --- a/jsonpb/jsonpb_test.go +++ b/jsonpb/jsonpb_test.go @@ -868,6 +868,7 @@ var unmarshalingShouldError = []struct { {"Timestamp containing invalid character", `{"ts": "2014-05-13T16:53:20\U005a"}`, &pb.KnownTypes{}}, {"StringValue containing invalid character", `{"str": "\U00004E16\U0000754C"}`, &pb.KnownTypes{}}, {"StructValue containing invalid character", `{"str": "\U00004E16\U0000754C"}`, &stpb.Struct{}}, + {"repeated proto3 enum with non array input", `{"rFunny":"PUNS"}`, &proto3pb.Message{RFunny: []proto3pb.Message_Humour{}}}, } func TestUnmarshalingBadInput(t *testing.T) {