diff --git a/src/google/protobuf/descriptor.cc b/src/google/protobuf/descriptor.cc index e93650e2ff..9a0e9f6cbd 100644 --- a/src/google/protobuf/descriptor.cc +++ b/src/google/protobuf/descriptor.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, diff --git a/src/google/protobuf/descriptor_unittest.cc b/src/google/protobuf/descriptor_unittest.cc index 39a8f1aa15..eb445af8b7 100644 --- a/src/google/protobuf/descriptor_unittest.cc +++ b/src/google/protobuf/descriptor_unittest.cc @@ -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();