Skip to content

Commit

Permalink
Put a 4k limit on preallocation to prevent OOM from malformed inputs.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 542579832
  • Loading branch information
protobuf-github-bot authored and googleberg committed Jul 13, 2023
1 parent 725a592 commit 173adca
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/google/protobuf/compiler/java/primitive_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -971,10 +971,12 @@ void RepeatedImmutablePrimitiveFieldGenerator::GenerateBuilderParsingCode(
void RepeatedImmutablePrimitiveFieldGenerator::
GenerateBuilderParsingCodeFromPacked(io::Printer* printer) const {
if (FixedSize(GetType(descriptor_)) != -1) {
// 4K limit on pre-allocations to prevent OOM from malformed input.
printer->Print(variables_,
"int length = input.readRawVarint32();\n"
"int limit = input.pushLimit(length);\n"
"ensure$capitalized_name$IsMutable(length / $fixed_size$);\n"
"int alloc = length > 4096 ? 4096 : length;\n"
"ensure$capitalized_name$IsMutable(alloc / $fixed_size$);\n"
"while (input.getBytesUntilLimit() > 0) {\n"
" $repeated_add$(input.read$capitalized_type$());\n"
"}\n"
Expand Down

0 comments on commit 173adca

Please sign in to comment.