Skip to content

Commit

Permalink
Check invalid type_id after casting it to uint32_t.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 536859974
  • Loading branch information
protobuf-github-bot authored and copybara-github committed May 31, 2023
1 parent 96c5923 commit d890126
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 60 deletions.
4 changes: 2 additions & 2 deletions objectivec/GPBAny.pbobjc.m

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions objectivec/GPBApi.pbobjc.m

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions objectivec/GPBDuration.pbobjc.m

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions objectivec/GPBEmpty.pbobjc.m

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions objectivec/GPBFieldMask.pbobjc.m

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions objectivec/GPBSourceContext.pbobjc.m

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions objectivec/GPBStruct.pbobjc.m

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions objectivec/GPBTimestamp.pbobjc.m

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions objectivec/GPBType.pbobjc.m

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 18 additions & 18 deletions objectivec/GPBWrappers.pbobjc.m

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions src/google/protobuf/extension_set_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,14 @@ const char* ExtensionSet::ParseMessageSetItemTmpl(
if (tag == WireFormatLite::kMessageSetTypeIdTag) {
uint64_t tmp;
ptr = ParseBigVarint(ptr, &tmp);
// We should fail parsing if type id is 0.
GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr && tmp != 0);
// We should fail parsing if type id is 0 after cast to uint32.
GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr &&
static_cast<uint32_t>(tmp) != 0);
if (state == State::kNoTag) {
type_id = tmp;
type_id = static_cast<uint32_t>(tmp);
state = State::kHasType;
} else if (state == State::kHasPayload) {
type_id = tmp;
type_id = static_cast<uint32_t>(tmp);
ExtensionInfo extension;
bool was_packed_on_wire;
if (!FindExtension(2, type_id, extendee, ctx, &extension,
Expand Down
9 changes: 5 additions & 4 deletions src/google/protobuf/wire_format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -671,13 +671,14 @@ struct WireFormat::MessageSetParser {
if (tag == WireFormatLite::kMessageSetTypeIdTag) {
uint64_t tmp;
ptr = ParseBigVarint(ptr, &tmp);
// We should fail parsing if type id is 0.
GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr && tmp != 0);
// We should fail parsing if type id is 0 after cast to uint32.
GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr &&
static_cast<uint32_t>(tmp) != 0);
if (state == State::kNoTag) {
type_id = tmp;
type_id = static_cast<uint32_t>(tmp);
state = State::kHasType;
} else if (state == State::kHasPayload) {
type_id = tmp;
type_id = static_cast<uint32_t>(tmp);
const FieldDescriptor* field;
if (ctx->data().pool == nullptr) {
field = reflection->FindKnownExtensionByNumber(type_id);
Expand Down
13 changes: 13 additions & 0 deletions src/google/protobuf/wire_format_unittest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,19 @@ TEST(WireFormatTest, MessageSetInvalidTypeId) {
EXPECT_FALSE(message.ParseFromArray(encoded, sizeof(encoded)));
}

TEST(WireFormatTest, MessageSetNonCanonInvalidTypeId) {
// "type_id" is 0 and should fail to parse. uint8_t is used to silence
// complaints about narrowing conversion.
const uint8_t encoded[] = {
013, // 1: SGROUP
032, 0, // 3:LEN 0
020, 0x80, 0x80, 0x80, 0x80, 020, // 2: long-form:2 0
014 // 1: EGROUP
};
PROTO2_WIREFORMAT_UNITTEST::TestMessageSet message;
EXPECT_FALSE(message.ParseFromArray(encoded, sizeof(encoded)));
}

namespace {
std::string BuildMessageSetItemStart() {
std::string data;
Expand Down

0 comments on commit d890126

Please sign in to comment.