Преглед изворни кода

Produce an error when unmarshaling text protos if a oneof field is set more than once.
PiperOrigin-RevId: 137859856

Googler пре 9 година
родитељ
комит
a66a4fa9a8
2 измењених фајлова са 16 додато и 1 уклоњено
  1. 5 1
      proto/text_parser.go
  2. 11 0
      proto/text_parser_test.go

+ 5 - 1
proto/text_parser.go

@@ -592,7 +592,11 @@ func (p *textParser) readStruct(sv reflect.Value, terminator string) error {
 			props = oop.Prop
 			nv := reflect.New(oop.Type.Elem())
 			dst = nv.Elem().Field(0)
-			sv.Field(oop.Field).Set(nv)
+			field := sv.Field(oop.Field)
+			if !field.IsNil() {
+				return p.errorf("oneof field '%s' is already set", name)
+			}
+			field.Set(nv)
 		}
 		if !dst.IsValid() {
 			return p.errorf("unknown field name %q in %v", name, st)

+ 11 - 0
proto/text_parser_test.go

@@ -635,6 +635,17 @@ func TestOneofParsing(t *testing.T) {
 	if !Equal(m, want) {
 		t.Errorf("\n got %v\nwant %v", m, want)
 	}
+
+	const inOverwrite = `name:"Shrek" number:42`
+	m = new(Communique)
+	testErr := "line 1.13: oneof field 'number' is already set"
+	if err := UnmarshalText(inOverwrite, m); err == nil {
+		t.Errorf("TestOneofParsing: Didn't get expected error: %v", testErr)
+	} else if err.Error() != testErr {
+		t.Errorf("TestOneofParsing: Incorrect error.\nHave: %v\nWant: %v",
+			err.Error(), testErr)
+	}
+
 }
 
 var benchInput string