Skip to content

Commit

Permalink
Forbid embedded nulls in json_name.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
haberman authored and copybara-github committed May 1, 2023
1 parent 65e047d commit b478a29
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/google/protobuf/descriptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions src/google/protobuf/descriptor_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit b478a29

Please sign in to comment.