Fix integer overflow on extension range parsing when specifying MAX_INT as an extension number.

PiperOrigin-RevId: 570084841
pull/14262/head
Protobuf Team Bot 1 year ago committed by Copybara-Service
parent 1312dcaae6
commit 0a452fd359
  1. 10
      src/google/protobuf/compiler/parser.cc
  2. 29
      src/google/protobuf/compiler/parser_unittest.cc

@ -1695,6 +1695,11 @@ bool Parser::ParseExtensions(DescriptorProto* message,
location, DescriptorProto::ExtensionRange::kStartFieldNumber);
start_token = input_->current();
DO(ConsumeInteger(&start, "Expected field number range."));
if (start == std::numeric_limits<int>::max()) {
RecordError("Field number out of bounds.");
return false;
}
}
if (TryConsume("to")) {
@ -1707,6 +1712,11 @@ bool Parser::ParseExtensions(DescriptorProto* message,
end = kMaxRangeSentinel - 1;
} else {
DO(ConsumeInteger(&end, "Expected integer."));
if (end == std::numeric_limits<int>::max()) {
RecordError("Field number out of bounds.");
return false;
}
}
} else {
LocationRecorder end_location(

@ -930,6 +930,35 @@ TEST_F(ParseMessageTest, CompoundExtensionRange) {
"}");
}
TEST_F(ParseMessageTest, MaxIntExtensionDoesNotOverflow) {
ExpectHasErrors(
R"(
syntax = "proto2";
message TestMessage {
extensions 2147483647;
}
)",
"3:31: Field number out of bounds.\n");
error_collector_.text_.clear();
ExpectHasErrors(
R"(
syntax = "proto2";
message TestMessage {
extensions 1 to 2147483647;
}
)",
"3:36: Field number out of bounds.\n");
error_collector_.text_.clear();
ExpectHasErrors(
R"(
syntax = "proto2";
message TestMessage {
extensions 2147483647 to 2147483647;
}
)",
"3:32: Field number out of bounds.\n");
}
TEST_F(ParseMessageTest, CompoundExtensionRangeWithOptions) {
ExpectParsesTo(
"message TestMessage {\n"

Loading…
Cancel
Save