add error check for duplicate oneof field names

PiperOrigin-RevId: 512029347
pull/13171/head
Eric Salo 2 years ago committed by Copybara-Service
parent 81d27ec0e5
commit 9253c42843
  1. 10
      upb/reflection/oneof_def.c
  2. 15
      upb/util/def_to_proto_test.cc

@ -128,11 +128,17 @@ void _upb_OneofDef_Insert(upb_DefBuilder* ctx, upb_OneofDef* o,
// inserting into the message's table. Unfortunately that step occurs after
// this one and moving things around could be tricky so let's leave it for
// a future refactoring.
const bool exists = upb_inttable_lookup(&o->itof, number, NULL);
if (UPB_UNLIKELY(exists)) {
const bool number_exists = upb_inttable_lookup(&o->itof, number, NULL);
if (UPB_UNLIKELY(number_exists)) {
_upb_DefBuilder_Errf(ctx, "oneof fields have the same number (%d)", number);
}
// TODO(salo): More redundant work happening here.
const bool name_exists = upb_strtable_lookup2(&o->ntof, name, size, NULL);
if (UPB_UNLIKELY(name_exists)) {
_upb_DefBuilder_Errf(ctx, "oneof fields have the same name (%s)", name);
}
const bool ok = upb_inttable_insert(&o->itof, number, v, ctx->arena) &&
upb_strtable_insert(&o->ntof, name, size, v, ctx->arena);
if (UPB_UNLIKELY(!ok)) {

@ -316,4 +316,19 @@ TEST(FuzzTest, RoundTripDescriptorRegression) {
})pb"));
}
// Multiple oneof fields which have the same name.
TEST(FuzzTest, RoundTripDescriptorRegressionOneofSameName) {
RoundTripDescriptor(ParseTextProtoOrDie(
R"pb(file {
name: "N"
package: ""
message_type {
name: "b"
field { name: "W" number: 1 type: TYPE_BYTES oneof_index: 0 }
field { name: "W" number: 17 type: TYPE_UINT32 oneof_index: 0 }
oneof_decl { name: "k" }
}
})pb"));
}
} // namespace upb_test

Loading…
Cancel
Save