encoding/prototext: fix crash in map parsing

Fuzzer-detected crash when parsing map values which should be messages,
but are not.

Fixes golang/protobuf#1003

Change-Id: Ib34b13d1a6fef7209e7c17dc5d7f4bd8a1ebac87
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/212397
Reviewed-by: Herbie Ong <herbie@google.com>
This commit is contained in:
Damien Neil 2019-12-22 09:46:57 -08:00
parent 5ba0c29655
commit 1a08d54978
2 changed files with 16 additions and 2 deletions

View File

@ -420,10 +420,14 @@ func unmarshalMapKey(input text.Value, fd pref.FieldDescriptor) (pref.MapKey, er
// unmarshalMapMessageValue unmarshals given message-type text.Value into a protoreflect.Map for
// the given MapKey.
func (o UnmarshalOptions) unmarshalMapMessageValue(input text.Value, pkey pref.MapKey, _ pref.FieldDescriptor, mmap pref.Map) error {
func (o UnmarshalOptions) unmarshalMapMessageValue(input text.Value, pkey pref.MapKey, fd pref.FieldDescriptor, mmap pref.Map) error {
var value [][2]text.Value
if input.Type() != 0 {
switch input.Type() {
case 0:
case text.Message:
value = input.Message()
default:
return errors.New("%v contains invalid value: %v", fd.FullName(), input)
}
val := mmap.NewValue()
if err := o.unmarshalMessage(value, val.Message()); err != nil {

View File

@ -948,6 +948,16 @@ int32_to_str: {
key: 100
value: 101
}
`,
wantErr: true,
}, {
desc: "map contains invalid message value",
inputMessage: &pb3.Maps{},
inputText: `
str_to_nested: {
key: "one"
value: 1
}
`,
wantErr: true,
}, {