Skip to content

Commit

Permalink
Initialize mini table subs, so that upb_MiniTable_Build creates stabl…
Browse files Browse the repository at this point in the history
…e unlinked table.

PiperOrigin-RevId: 479445405
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Oct 6, 2022
1 parent b33fd88 commit bc2022a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion upb/mini_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,9 @@ static const char* upb_MtDecoder_ParseModifier(upb_MtDecoder* d,

static void upb_MtDecoder_AllocateSubs(upb_MtDecoder* d, uint32_t sub_count) {
size_t subs_bytes = sizeof(*d->table->subs) * sub_count;
d->table->subs = upb_Arena_Malloc(d->arena, subs_bytes);
void* subs = upb_Arena_Malloc(d->arena, subs_bytes);
memset(subs, 0, subs_bytes);
d->table->subs = subs;
upb_MtDecoder_CheckOutOfMemory(d, d->table->subs);
}

Expand Down
16 changes: 16 additions & 0 deletions upb/mini_table_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,22 @@ TEST(MiniTableEnumTest, Enum) {
}
}

TEST_P(MiniTableTest, SubsInitializedToNull) {
upb::Arena arena;
upb::MtDataEncoder e;
// Create mini table with 2 message fields.
ASSERT_TRUE(e.StartMessage(0));
ASSERT_TRUE(e.PutField(kUpb_FieldType_Message, 15, 0));
ASSERT_TRUE(e.PutField(kUpb_FieldType_Message, 16, 0));
upb::Status status;
upb_MiniTable* table = upb_MiniTable_Build(
e.data().data(), e.data().size(), GetParam(), arena.ptr(), status.ptr());
ASSERT_NE(nullptr, table);
EXPECT_EQ(table->field_count, 2);
EXPECT_EQ(table->subs[0].submsg, nullptr);
EXPECT_EQ(table->subs[1].submsg, nullptr);
}

TEST(MiniTableEnumTest, PositiveAndNegative) {
upb::Arena arena;
upb::MtDataEncoder e;
Expand Down

0 comments on commit bc2022a

Please sign in to comment.