From e03e9f4b5774c0ffe04140d83bbdb532863b1720 Mon Sep 17 00:00:00 2001 From: Brian Duff Date: Wed, 9 Oct 2013 12:23:16 -0700 Subject: Protect against null repeated fields. There's no distinction between a repeated field being null and being empty. In both cases, nothing is sent on the wire. Clients might for whatever reason inadvertently set a repeated field to null, so protect against that and treat it just as if the field was empty. Change-Id: Ic3846f7f2189d6cfff6f8ef3ca217daecc3c8be7 --- .../compiler/javanano/javanano_enum_field.cc | 4 ++-- .../compiler/javanano/javanano_message_field.cc | 20 +++++++++++++------- .../compiler/javanano/javanano_primitive_field.cc | 19 ++++++++++--------- 3 files changed, 25 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/google/protobuf/compiler/javanano/javanano_enum_field.cc b/src/google/protobuf/compiler/javanano/javanano_enum_field.cc index 420d3c2..cdb3d09 100644 --- a/src/google/protobuf/compiler/javanano/javanano_enum_field.cc +++ b/src/google/protobuf/compiler/javanano/javanano_enum_field.cc @@ -293,7 +293,7 @@ GenerateMergingCode(io::Printer* printer) const { void RepeatedEnumFieldGenerator:: GenerateSerializationCode(io::Printer* printer) const { printer->Print(variables_, - "if (this.$name$.length > 0) {\n"); + "if (this.$name$ != null && this.$name$.length > 0) {\n"); printer->Indent(); if (descriptor_->options().packed()) { @@ -317,7 +317,7 @@ GenerateSerializationCode(io::Printer* printer) const { void RepeatedEnumFieldGenerator:: GenerateSerializedSizeCode(io::Printer* printer) const { printer->Print(variables_, - "if (this.$name$.length > 0) {\n"); + "if (this.$name$ != null && this.$name$.length > 0) {\n"); printer->Indent(); printer->Print(variables_, diff --git a/src/google/protobuf/compiler/javanano/javanano_message_field.cc b/src/google/protobuf/compiler/javanano/javanano_message_field.cc index 277eb4e..04f1c14 100644 --- a/src/google/protobuf/compiler/javanano/javanano_message_field.cc +++ b/src/google/protobuf/compiler/javanano/javanano_message_field.cc @@ -233,9 +233,11 @@ GenerateMergingCode(io::Printer* printer) const { printer->Print(variables_, "int arrayLength = com.google.protobuf.nano.WireFormatNano" " .getRepeatedFieldArrayLength(input, $tag$);\n" - "int i = this.$name$.length;\n" + "int i = this.$name$ == null ? 0 : this.$name$.length;\n" "$type$[] newArray = new $type$[i + arrayLength];\n" - "System.arraycopy(this.$name$, 0, newArray, 0, i);\n" + "if (this.$name$ != null) {\n" + " System.arraycopy(this.$name$, 0, newArray, 0, i);\n" + "}\n" "this.$name$ = newArray;\n" "for (; i < this.$name$.length - 1; i++) {\n" " this.$name$[i] = new $type$();\n"); @@ -266,17 +268,21 @@ GenerateMergingCode(io::Printer* printer) const { void RepeatedMessageFieldGenerator:: GenerateSerializationCode(io::Printer* printer) const { printer->Print(variables_, - "for ($type$ element : this.$name$) {\n" - " output.write$group_or_message$($number$, element);\n" + "if (this.$name$ != null) {\n" + " for ($type$ element : this.$name$) {\n" + " output.write$group_or_message$($number$, element);\n" + " }\n" "}\n"); } void RepeatedMessageFieldGenerator:: GenerateSerializedSizeCode(io::Printer* printer) const { printer->Print(variables_, - "for ($type$ element : this.$name$) {\n" - " size += com.google.protobuf.nano.CodedOutputByteBufferNano\n" - " .compute$group_or_message$Size($number$, element);\n" + "if (this.$name$ != null) {\n" + " for ($type$ element : this.$name$) {\n" + " size += com.google.protobuf.nano.CodedOutputByteBufferNano\n" + " .compute$group_or_message$Size($number$, element);\n" + " }\n" "}\n"); } diff --git a/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc b/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc index f5e27d6..8097be8 100644 --- a/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc +++ b/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc @@ -570,17 +570,15 @@ GenerateRepeatedDataSizeCode(io::Printer* printer) const { void RepeatedPrimitiveFieldGenerator:: GenerateSerializationCode(io::Printer* printer) const { + printer->Print(variables_, + "if (this.$name$ != null && this.$name$.length > 0) {\n"); + printer->Indent(); + if (descriptor_->options().packed()) { - printer->Print(variables_, - "if (this.$name$.length > 0) {\n"); - printer->Indent(); GenerateRepeatedDataSizeCode(printer); - printer->Outdent(); - printer->Print(variables_, - " output.writeRawVarint32($tag$);\n" - " output.writeRawVarint32(dataSize);\n" - "}\n"); printer->Print(variables_, + "output.writeRawVarint32($tag$);\n" + "output.writeRawVarint32(dataSize);\n" "for ($type$ element : this.$name$) {\n" " output.write$capitalized_type$NoTag(element);\n" "}\n"); @@ -590,12 +588,15 @@ GenerateSerializationCode(io::Printer* printer) const { " output.write$capitalized_type$($number$, element);\n" "}\n"); } + + printer->Outdent(); + printer->Print("}\n"); } void RepeatedPrimitiveFieldGenerator:: GenerateSerializedSizeCode(io::Printer* printer) const { printer->Print(variables_, - "if (this.$name$.length > 0) {\n"); + "if (this.$name$ != null && this.$name$.length > 0) {\n"); printer->Indent(); GenerateRepeatedDataSizeCode(printer); -- cgit v1.1