Fail to parse message sets if type_id is zero.

PiperOrigin-RevId: 534517343
pull/12838/head
Protobuf Team Bot 2 years ago committed by Copybara-Service
parent 86fc32cedc
commit bc1b1f6e53
  1. 3
      src/google/protobuf/extension_set_inl.h
  2. 3
      src/google/protobuf/wire_format.cc
  3. 3
      src/google/protobuf/wire_format_lite.h
  4. 25
      src/google/protobuf/wire_format_unittest.inc

@ -215,7 +215,8 @@ const char* ExtensionSet::ParseMessageSetItemTmpl(
if (tag == WireFormatLite::kMessageSetTypeIdTag) {
uint64_t tmp;
ptr = ParseBigVarint(ptr, &tmp);
GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
// We should fail parsing if type id is 0.
GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr && tmp != 0);
if (state == State::kNoTag) {
type_id = tmp;
state = State::kHasType;

@ -671,7 +671,8 @@ struct WireFormat::MessageSetParser {
if (tag == WireFormatLite::kMessageSetTypeIdTag) {
uint64_t tmp;
ptr = ParseBigVarint(ptr, &tmp);
GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
// We should fail parsing if type id is 0.
GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr && tmp != 0);
if (state == State::kNoTag) {
type_id = tmp;
state = State::kHasType;

@ -1890,7 +1890,8 @@ bool ParseMessageSetItemImpl(io::CodedInputStream* input, MS ms) {
switch (tag) {
case WireFormatLite::kMessageSetTypeIdTag: {
uint32_t type_id;
if (!input->ReadVarint32(&type_id)) return false;
// We should fail parsing if type id is 0.
if (!input->ReadVarint32(&type_id) || type_id == 0) return false;
if (state == State::kNoTag) {
last_type_id = type_id;
state = State::kHasType;

@ -579,6 +579,31 @@ TEST(WireFormatTest, ParseMessageSet) {
EXPECT_EQ(message_set.DebugString(), dynamic_message_set.DebugString());
}
TEST(WireFormatTest, MessageSetUnknownButValidTypeId) {
const char encoded[] = {
013, // 1: SGROUP
032, 2, // 3:LEN 2
010, 0, // 1:0
020, 4, // 2:4
014 // 1: EGROUP
};
PROTO2_WIREFORMAT_UNITTEST::TestMessageSet message;
EXPECT_TRUE(message.ParseFromArray(encoded, sizeof(encoded)));
}
TEST(WireFormatTest, MessageSetInvalidTypeId) {
// "type_id" is 0 and should fail to parse.
const char encoded[] = {
013, // 1: SGROUP
032, 2, // 3:LEN 2
010, 0, // 1:0
020, 0, // 2:0
014 // 1: EGROUP
};
PROTO2_WIREFORMAT_UNITTEST::TestMessageSet message;
EXPECT_FALSE(message.ParseFromArray(encoded, sizeof(encoded)));
}
namespace {
std::string BuildMessageSetItemStart() {
std::string data;

Loading…
Cancel
Save