Skip to content

Commit 173adca

Browse files
protobuf-github-botgoogleberg
authored andcommitted
Put a 4k limit on preallocation to prevent OOM from malformed inputs.
PiperOrigin-RevId: 542579832
1 parent 725a592 commit 173adca

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/google/protobuf/compiler/java/primitive_field.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -971,10 +971,12 @@ void RepeatedImmutablePrimitiveFieldGenerator::GenerateBuilderParsingCode(
971971
void RepeatedImmutablePrimitiveFieldGenerator::
972972
GenerateBuilderParsingCodeFromPacked(io::Printer* printer) const {
973973
if (FixedSize(GetType(descriptor_)) != -1) {
974+
// 4K limit on pre-allocations to prevent OOM from malformed input.
974975
printer->Print(variables_,
975976
"int length = input.readRawVarint32();\n"
976977
"int limit = input.pushLimit(length);\n"
977-
"ensure$capitalized_name$IsMutable(length / $fixed_size$);\n"
978+
"int alloc = length > 4096 ? 4096 : length;\n"
979+
"ensure$capitalized_name$IsMutable(alloc / $fixed_size$);\n"
978980
"while (input.getBytesUntilLimit() > 0) {\n"
979981
" $repeated_add$(input.read$capitalized_type$());\n"
980982
"}\n"

0 commit comments

Comments
 (0)