Skip to content

Commit 139fff6

Browse files
Rachel Goldfingercopybara-github
authored andcommitted
Treat mismatched length-delimited repeated Group tags as unknown fields in upb.
PiperOrigin-RevId: 919206177
1 parent 37b2497 commit 139fff6

4 files changed

Lines changed: 49 additions & 3 deletions

File tree

upb/wire/decode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ static int _upb_Decoder_GetDelimitedOp(upb_Decoder* d, const upb_MiniTable* mt,
861861
[kRepeatedBase + kUpb_FieldType_Fixed32] = OP_FIXPCK_LG2(2),
862862
[kRepeatedBase + kUpb_FieldType_Bool] = OP_VARPCK_LG2(0),
863863
[kRepeatedBase + kUpb_FieldType_String] = kUpb_DecodeOp_String,
864-
[kRepeatedBase + kUpb_FieldType_Group] = kUpb_DecodeOp_SubMessage,
864+
[kRepeatedBase + kUpb_FieldType_Group] = kUpb_DecodeOp_UnknownField,
865865
[kRepeatedBase + kUpb_FieldType_Message] = kUpb_DecodeOp_SubMessage,
866866
[kRepeatedBase + kUpb_FieldType_Bytes] = kUpb_DecodeOp_Bytes,
867867
[kRepeatedBase + kUpb_FieldType_UInt32] = OP_VARPCK_LG2(2),

upb/wire/decode_test.cc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,43 @@ TEST(DecodeTest, MaxDepthPayloadParsesSuccessfully) {
471471
}
472472
}
473473

474+
TEST(DecodeTest, DecodeGroupFieldFromDelimitedWireFormatAsUnknown) {
475+
upb::Arena mt_arena;
476+
upb::Arena msg_arena;
477+
478+
// 1. Create Parent MiniTable containing a repeated Group field directly.
479+
auto [parent_mt, parent_field] =
480+
test::MiniTable::MakeSingleFieldTable<test::field_types::Group>(
481+
5, kUpb_DecodeFast_Repeated, mt_arena.ptr());
482+
483+
// 2. Build length-delimited wire payload for Group field 5:
484+
// Tag 5 Delimited = 42 (0x2a), length = 2, child field 1 = 123 ("\x08\x7b").
485+
std::string payload("\x2a\x02\x08\x7b", 4);
486+
487+
// 3. Parse the payload into Parent Message.
488+
upb_Message* parent_msg = upb_Message_New(parent_mt, msg_arena.ptr());
489+
upb_DecodeStatus result =
490+
upb_Decode(payload.data(), payload.size(), parent_msg, parent_mt, nullptr,
491+
0, msg_arena.ptr());
492+
493+
// 4. Verify parsing succeeded cleanly.
494+
ASSERT_EQ(result, kUpb_DecodeStatus_Ok) << upb_DecodeStatus_String(result);
495+
496+
// 5. Verify repeated Group field 5 was NOT populated as a known field.
497+
const upb_Array* arr = upb_Message_GetArray(parent_msg, parent_field);
498+
EXPECT_EQ(arr, nullptr);
499+
500+
// 6. Verify the wire payload was instead preserved inside the Unknown field
501+
// set.
502+
EXPECT_TRUE(upb_Message_HasUnknown(parent_msg));
503+
504+
uintptr_t iter = kUpb_Message_UnknownBegin;
505+
upb_StringView data;
506+
ASSERT_TRUE(upb_Message_NextUnknown(parent_msg, &data, &iter));
507+
EXPECT_EQ(absl::string_view(data.data, data.size), payload);
508+
EXPECT_FALSE(upb_Message_NextUnknown(parent_msg, &data, &iter));
509+
}
510+
474511
} // namespace
475512

476513
} // namespace test

upb/wire/test_util/field_types.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,16 @@ struct ClosedEnum {
293293
}
294294
};
295295

296-
// TODO: Group
296+
struct Group {
297+
using Value = std::string;
298+
static constexpr upb_FieldType kFieldType = kUpb_FieldType_Group;
299+
static constexpr absl::string_view kName = "Group";
300+
static constexpr upb_DecodeFast_Type kFastType = kUpb_DecodeFast_Message;
301+
302+
static wire_types::WireValue WireValue(std::string value) {
303+
return wire_types::Delimited(value);
304+
}
305+
};
297306

298307
} // namespace field_types
299308

upb/wire/test_util/make_mini_table.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ MiniTable::MakeSingleFieldTable(int field_number, upb_FieldType type,
100100
ABSL_CHECK(ok);
101101
}
102102
#if UPB_FASTTABLE
103-
if (field_number < (1 << 11)) {
103+
if (field_number < (1 << 11) && type != kUpb_FieldType_Group) {
104104
ABSL_CHECK_EQ(HasFastTableEntry(table, field),
105105
UPB_DECODEFAST_ISENABLED(fast_type, cardinality,
106106
kUpb_DecodeFast_Tag1Byte))

0 commit comments

Comments
 (0)