Skip to content

Commit b478a29

Browse files
habermancopybara-github
authored andcommitted
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
1 parent 65e047d commit b478a29

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/google/protobuf/descriptor.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7336,6 +7336,12 @@ void DescriptorBuilder::ValidateFieldOptions(
73367336
"option json_name is not allowed on extension fields.");
73377337
}
73387338

7339+
if (absl::StrContains(field->json_name(), '\0')) {
7340+
AddError(field->full_name(), proto,
7341+
DescriptorPool::ErrorCollector::OPTION_NAME,
7342+
"json_name cannot have embedded null characters.");
7343+
}
7344+
73397345
}
73407346

73417347
void DescriptorBuilder::ValidateEnumOptions(const EnumDescriptor* enm,

src/google/protobuf/descriptor_unittest.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5892,6 +5892,24 @@ TEST_F(ValidationErrorTest, JsonNameOptionOnExtensions) {
58925892
"extension fields.\n");
58935893
}
58945894

5895+
TEST_F(ValidationErrorTest, JsonNameEmbeddedNull) {
5896+
BuildFileWithErrors(
5897+
"name: \"foo.proto\" "
5898+
"package: \"foo\" "
5899+
"message_type {"
5900+
" name: \"Foo\""
5901+
" field {"
5902+
" name: \"value\""
5903+
" number: 10"
5904+
" label: LABEL_OPTIONAL"
5905+
" type: TYPE_INT32"
5906+
" json_name: \"embedded\\000null\""
5907+
" }"
5908+
"}",
5909+
"foo.proto: foo.Foo.value: OPTION_NAME: json_name cannot have embedded "
5910+
"null characters.\n");
5911+
}
5912+
58955913
TEST_F(ValidationErrorTest, DuplicateExtensionFieldNumber) {
58965914
BuildDescriptorMessagesInTestPool();
58975915

0 commit comments

Comments
 (0)