Forbid embedded nulls in `json_name`.

While embedded nulls are technically allowed in JSON strings (and thus as object keys), they put undesirable constraints on implementations.  Embedded nulls require that string length is stored explicitly, which wastes memory to accommodate a case that virtually nobody wants or needs.

PiperOrigin-RevId: 528484333
pull/12532/head
Joshua Haberman 2 years ago committed by Copybara-Service
parent 65e047d098
commit b478a29bf0
  1. 6
      src/google/protobuf/descriptor.cc
  2. 18
      src/google/protobuf/descriptor_unittest.cc

@ -7336,6 +7336,12 @@ void DescriptorBuilder::ValidateFieldOptions(
"option json_name is not allowed on extension fields.");
}
if (absl::StrContains(field->json_name(), '\0')) {
AddError(field->full_name(), proto,
DescriptorPool::ErrorCollector::OPTION_NAME,
"json_name cannot have embedded null characters.");
}
}
void DescriptorBuilder::ValidateEnumOptions(const EnumDescriptor* enm,

@ -5892,6 +5892,24 @@ TEST_F(ValidationErrorTest, JsonNameOptionOnExtensions) {
"extension fields.\n");
}
TEST_F(ValidationErrorTest, JsonNameEmbeddedNull) {
BuildFileWithErrors(
"name: \"foo.proto\" "
"package: \"foo\" "
"message_type {"
" name: \"Foo\""
" field {"
" name: \"value\""
" number: 10"
" label: LABEL_OPTIONAL"
" type: TYPE_INT32"
" json_name: \"embedded\\000null\""
" }"
"}",
"foo.proto: foo.Foo.value: OPTION_NAME: json_name cannot have embedded "
"null characters.\n");
}
TEST_F(ValidationErrorTest, DuplicateExtensionFieldNumber) {
BuildDescriptorMessagesInTestPool();

Loading…
Cancel
Save