diff --git a/upb/mini_table.c b/upb/mini_table.c index 409022ab35..011aa2c6d5 100644 --- a/upb/mini_table.c +++ b/upb/mini_table.c @@ -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); } diff --git a/upb/mini_table_test.cc b/upb/mini_table_test.cc index 24efe5b519..ec65f251b6 100644 --- a/upb/mini_table_test.cc +++ b/upb/mini_table_test.cc @@ -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;