diff options
author | Brian Duff <bduff@google.com> | 2013-10-09 12:23:16 -0700 |
---|---|---|
committer | Brian Duff <bduff@google.com> | 2013-10-14 23:14:58 -0700 |
commit | e03e9f4b5774c0ffe04140d83bbdb532863b1720 (patch) | |
tree | 33912f4d1966adb1bd4c9813dbc1c2276b1ac49d /src | |
parent | 39cee9f1f5cd513a53ac5100eb208ed431f99af0 (diff) | |
download | external_protobuf-e03e9f4b5774c0ffe04140d83bbdb532863b1720.zip external_protobuf-e03e9f4b5774c0ffe04140d83bbdb532863b1720.tar.gz external_protobuf-e03e9f4b5774c0ffe04140d83bbdb532863b1720.tar.bz2 |
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
Diffstat (limited to 'src')
3 files changed, 25 insertions, 18 deletions
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); |