Change the logic for reserved field name verification to force a fully

qualified type name (ie starts with `.`).
This way we guarantee that the spelling the `.proto` file will be the same as
the spelling we see after crosslinking types.
Otherwise, the spellings might differ and it makes it harder to determine if
the name in the allowlist matches the name in the `.proto` file.

PiperOrigin-RevId: 497997383
pull/11410/head
Protobuf Team Bot 2 years ago committed by Copybara-Service
parent 81d90d1eab
commit 29a6a2189a
  1. 8
      src/google/protobuf/descriptor.cc
  2. 47
      src/google/protobuf/descriptor_unittest.cc

@ -5860,9 +5860,13 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto,
AddError(result->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER, AddError(result->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER,
absl::Substitute( absl::Substitute(
"Field numbers $0 through $1 are reserved for the protocol " "Field numbers $0 through $1 are reserved for the protocol "
"buffer library implementation.", "buffer library implementation$2.",
FieldDescriptor::kFirstReservedNumber, FieldDescriptor::kFirstReservedNumber,
FieldDescriptor::kLastReservedNumber)); FieldDescriptor::kLastReservedNumber,
absl::StartsWith(proto.type_name(), ".")
? ""
: ", and the type name must be fully qualified with a `.` "
"prefix"));
} }
if (is_extension) { if (is_extension) {

@ -4585,21 +4585,42 @@ TEST_F(ValidationErrorTest, HugeFieldNumber) {
TEST_F(ValidationErrorTest, ReservedFieldNumber) { TEST_F(ValidationErrorTest, ReservedFieldNumber) {
BuildFileWithErrors( BuildFileWithErrors(
"name: \"foo.proto\" " R"pb(
"message_type {" name: "foo.proto"
" name: \"Foo\"" message_type {
" field {name:\"foo\" number: 18999 label:LABEL_OPTIONAL " name: "Foo"
"type:TYPE_INT32 }" field {
" field {name:\"bar\" number: 19000 label:LABEL_OPTIONAL " name: "foo"
"type:TYPE_INT32 }" number: 18999
" field {name:\"baz\" number: 19999 label:LABEL_OPTIONAL " label: LABEL_OPTIONAL
"type:TYPE_INT32 }" type: TYPE_INT32
" field {name:\"moo\" number: 20000 label:LABEL_OPTIONAL " }
"type:TYPE_INT32 }" field {
"}", name: "bar"
number: 19000
label: LABEL_OPTIONAL
type: TYPE_MESSAGE
type_name: "Foo"
}
field {
name: "baz"
number: 19999
label: LABEL_OPTIONAL
type: TYPE_MESSAGE
type_name: ".Foo"
}
field {
name: "moo"
number: 20000
label: LABEL_OPTIONAL
type: TYPE_INT32
}
}
)pb",
"foo.proto: Foo.bar: NUMBER: Field numbers 19000 through 19999 are " "foo.proto: Foo.bar: NUMBER: Field numbers 19000 through 19999 are "
"reserved for the protocol buffer library implementation.\n" "reserved for the protocol buffer library implementation, and the type "
"name must be fully qualified with a `.` prefix.\n"
"foo.proto: Foo.baz: NUMBER: Field numbers 19000 through 19999 are " "foo.proto: Foo.baz: NUMBER: Field numbers 19000 through 19999 are "
"reserved for the protocol buffer library implementation.\n" "reserved for the protocol buffer library implementation.\n"
"foo.proto: Foo: NUMBER: Suggested field numbers for Foo: 1, 2\n"); "foo.proto: Foo: NUMBER: Suggested field numbers for Foo: 1, 2\n");

Loading…
Cancel
Save