Skip to content

Commit

Permalink
Fix upb_MiniTable_GetOneof(miniTable, field) to work correctly if `fi…
Browse files Browse the repository at this point in the history
…eld` is the very first field in the proto.

PiperOrigin-RevId: 580285127
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Nov 7, 2023
1 parent b837d17 commit e6ea44c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion upb/mini_table/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions upb/test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 10 additions & 0 deletions upb/test/proto3_test.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
10 changes: 10 additions & 0 deletions upb/test/test_mini_table_oneof.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 =
Expand Down

0 comments on commit e6ea44c

Please sign in to comment.