Fix upb_MiniTable_GetOneof(miniTable, field) to work correctly if `field` is the very first field in the proto.

PiperOrigin-RevId: 580285127
pull/14668/head
Protobuf Team Bot 1 year ago committed by Copybara-Service
parent b837d17d2c
commit e6ea44c911
  1. 2
      upb/mini_table/message.c
  2. 1
      upb/test/BUILD
  3. 10
      upb/test/proto3_test.proto
  4. 10
      upb/test/test_mini_table_oneof.cc

@ -55,7 +55,7 @@ const upb_MiniTableField* upb_MiniTable_GetOneof(const upb_MiniTable* m,
}
const upb_MiniTableField* ptr = &m->fields[0];
const upb_MiniTableField* end = &m->fields[m->field_count];
while (++ptr < end) {
for (; ptr < end; ptr++) {
if (ptr->presence == (*f).presence) {
return ptr;
}

@ -239,6 +239,7 @@ cc_test(
],
deps = [
":empty_upb_proto_reflection",
":proto3_test_upb_proto",
":test_messages_proto2_upb_minitable",
":test_upb_proto",
"@com_google_googletest//:gtest_main",

@ -27,3 +27,13 @@ message TestMessage3 {
optional TestMessage3 msg = 5;
repeated TestMessage3 r_msg = 6;
}
// See the InitialFieldOneOf test in test_mini_table_oneof.cc.
message TestOneOfInitialField {
oneof oneof_field {
int32 a = 1;
uint32 b = 2;
}
float c = 3;
}

@ -9,6 +9,7 @@
#include "google/protobuf/test_messages_proto2.upb_minitable.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/message.h"
#include "upb/test/proto3_test.upb.h"
// Must be last.
#include "upb/port/def.inc"
@ -29,6 +30,15 @@ TEST(MiniTableOneofTest, OneOfIteratorProto2) {
} while (upb_MiniTable_NextOneofField(google_protobuf_table, &ptr));
}
TEST(MiniTableOneofTest, InitialFieldOneOf) {
const upb_MiniTable* table = &upb__test__TestOneOfInitialField_msg_init;
const upb_MiniTableField* field = upb_MiniTable_FindFieldByNumber(table, 1);
ASSERT_TRUE(field != nullptr);
const upb_MiniTableField* ptr = upb_MiniTable_GetOneof(table, field);
EXPECT_TRUE(ptr == field);
}
TEST(MiniTableOneofTest, InitialFieldNotOneOf) {
constexpr int test_field_number = 1; // optional int that is not a oneof
const upb_MiniTable* google_protobuf_table =

Loading…
Cancel
Save