Skip to content

Commit 3c4f0e5

Browse files
habermancopybara-github
authored andcommitted
Internal change
PiperOrigin-RevId: 952905580
1 parent cb44083 commit 3c4f0e5

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

upb/wire/decode_test.cc

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,61 @@ TEST(DecodeTest, MessageSetConsecutiveUnknowns) {
731731
}
732732
}
733733

734+
TEST(DecodeTest, FieldZeroRejected) {
735+
Arena mt_arena;
736+
737+
// 1. Empty message with field 0 varint payload.
738+
{
739+
upb_MiniTable* empty_mt =
740+
(upb_MiniTable*)upb_Arena_Malloc(mt_arena.ptr(), sizeof(upb_MiniTable));
741+
memset(empty_mt, 0, sizeof(upb_MiniTable));
742+
empty_mt->UPB_PRIVATE(size) = sizeof(upb_Message);
743+
empty_mt->UPB_ONLYBITS(field_count) = 0;
744+
745+
std::string payload("\x00\x00", 2);
746+
for (int options : GetDecodeOptionsToTest()) {
747+
Arena msg_arena;
748+
upb_Message* msg = upb_Message_New(empty_mt, msg_arena.ptr());
749+
upb_DecodeStatus result =
750+
upb_Decode(payload.data(), payload.size(), msg, empty_mt, nullptr,
751+
options, msg_arena.ptr());
752+
EXPECT_EQ(result, kUpb_DecodeStatus_Malformed);
753+
}
754+
}
755+
756+
// 2. Field 0 varint inside unknown group.
757+
{
758+
auto [mt, field] = MiniTable::MakeSingleFieldTable<field_types::Int32>(
759+
1, kUpb_DecodeFast_Scalar, mt_arena.ptr());
760+
761+
// Field 2 (StartGroup) containing Field 0 varint.
762+
std::string payload("\x13\x00\x00\x14", 4);
763+
for (int options : GetDecodeOptionsToTest()) {
764+
Arena msg_arena;
765+
upb_Message* msg = upb_Message_New(mt, msg_arena.ptr());
766+
upb_DecodeStatus result =
767+
upb_Decode(payload.data(), payload.size(), msg, mt, nullptr, options,
768+
msg_arena.ptr());
769+
EXPECT_EQ(result, kUpb_DecodeStatus_Malformed);
770+
}
771+
}
772+
773+
// 3. Field 0 varint inside MessageSet item.
774+
{
775+
const upb_MiniTable* mset_mt = &upb_0decode_0test__TestMessageSet_msg_init;
776+
// Field 1 (StartGroup for MessageSet Item) containing Field 0 varint.
777+
std::string payload("\x0b\x00\x00\x0c", 4);
778+
for (int options : GetDecodeOptionsToTest()) {
779+
Arena msg_arena;
780+
upb_Message* msg = upb_Message_New(mset_mt, msg_arena.ptr());
781+
upb_DecodeStatus result =
782+
upb_Decode(payload.data(), payload.size(), msg, mset_mt, nullptr,
783+
options, msg_arena.ptr());
784+
EXPECT_EQ(result, kUpb_DecodeStatus_Malformed);
785+
}
786+
}
787+
}
788+
734789
} // namespace
735790

736791
} // namespace test

upb/wire/reader.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ UPB_INLINE const char* upb_WireReader_SkipGroup(
128128
UPB_FORCEINLINE const char* _upb_WireReader_SkipValueForceInline(
129129
const char* ptr, uint32_t tag, int depth_limit,
130130
upb_EpsCopyInputStream* stream) {
131+
if (UPB_UNLIKELY((tag >> 3) == 0)) {
132+
return UPB_PRIVATE(upb_EpsCopyInputStream_ReturnError)(stream);
133+
}
131134
switch (upb_WireReader_GetWireType(tag)) {
132135
case kUpb_WireType_Varint:
133136
return upb_WireReader_SkipVarint(ptr, stream);

0 commit comments

Comments
 (0)