validate that reserved range start is before end; fix location information for existing similar check for enum reserved ranges

pull/13474/head
Josh Humphries 1 year ago
parent 421ce84e5a
commit 661adc745a
  1. 4
      src/google/protobuf/compiler/parser.cc
  2. 25
      src/google/protobuf/compiler/parser_unittest.cc
  3. 4
      src/google/protobuf/descriptor.cc

@ -1818,6 +1818,8 @@ bool Parser::ParseReservedNumbers(DescriptorProto* message,
LocationRecorder location(parent_location, message->reserved_range_size()); LocationRecorder location(parent_location, message->reserved_range_size());
DescriptorProto::ReservedRange* range = message->add_reserved_range(); DescriptorProto::ReservedRange* range = message->add_reserved_range();
location.RecordLegacyLocation(range,
DescriptorPool::ErrorCollector::NUMBER);
int start, end; int start, end;
io::Tokenizer::Token start_token; io::Tokenizer::Token start_token;
{ {
@ -1895,6 +1897,8 @@ bool Parser::ParseReservedNumbers(EnumDescriptorProto* proto,
LocationRecorder location(parent_location, proto->reserved_range_size()); LocationRecorder location(parent_location, proto->reserved_range_size());
EnumDescriptorProto::EnumReservedRange* range = proto->add_reserved_range(); EnumDescriptorProto::EnumReservedRange* range = proto->add_reserved_range();
location.RecordLegacyLocation(range,
DescriptorPool::ErrorCollector::NUMBER);
int start, end; int start, end;
io::Tokenizer::Token start_token; io::Tokenizer::Token start_token;
{ {

@ -2038,6 +2038,22 @@ TEST_F(ParserValidationErrorTest, ExtensionRangeNumberError) {
"1:13: Suggested field numbers for Foo: 1\n"); "1:13: Suggested field numbers for Foo: 1\n");
} }
TEST_F(ParserValidationErrorTest, ExtensionRangeNumberOrderError) {
ExpectHasValidationErrors(
"message Foo {\n"
" extensions 2 to 1;\n"
"}\n",
"1:13: Extension range end number must be greater than start number.\n");
}
TEST_F(ParserValidationErrorTest, ReservedRangeError) {
ExpectHasValidationErrors(
"message Foo {\n"
" reserved 2 to 1;\n"
"}\n",
"1:11: Reserved range end number must be greater than start number.\n");
}
TEST_F(ParserValidationErrorTest, Proto3ExtensionError) { TEST_F(ParserValidationErrorTest, Proto3ExtensionError) {
ExpectHasValidationErrors( ExpectHasValidationErrors(
"syntax = 'proto3';\n" "syntax = 'proto3';\n"
@ -2234,6 +2250,15 @@ TEST_F(ParserValidationErrorTest, EnumValueAliasError) {
"definition. The next available enum value is 2.\n"); "definition. The next available enum value is 2.\n");
} }
TEST_F(ParserValidationErrorTest, EnumReservedRangeError) {
ExpectHasValidationErrors(
"enum Foo {\n"
" BAR = 1;\n"
" reserved 2 to 1;\n"
"}\n",
"2:11: Reserved range end number must be greater than start number.\n");
}
TEST_F(ParserValidationErrorTest, ExplicitlyMapEntryError) { TEST_F(ParserValidationErrorTest, ExplicitlyMapEntryError) {
ExpectHasValidationErrors( ExpectHasValidationErrors(
"message Foo {\n" "message Foo {\n"

@ -6677,6 +6677,10 @@ void DescriptorBuilder::BuildReservedRange(
AddError(parent->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER, AddError(parent->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER,
"Reserved numbers must be positive integers."); "Reserved numbers must be positive integers.");
} }
if (result->start >= result->end) {
AddError(parent->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER,
"Reserved range end number must be greater than start number.");
}
} }
void DescriptorBuilder::BuildReservedRange( void DescriptorBuilder::BuildReservedRange(

Loading…
Cancel
Save