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 | 34547de99cdcc76ed2f5977371b3e7ef8d9dcc0e (patch) | |
tree | 5be992124deac027ef0e5298d5c1bd94b1a5194f /java/src | |
parent | f18868306fa7acca1b9a7ecbcf1bc758295cd171 (diff) | |
download | protobuf-34547de99cdcc76ed2f5977371b3e7ef8d9dcc0e.tar.gz protobuf-34547de99cdcc76ed2f5977371b3e7ef8d9dcc0e.tar.bz2 protobuf-34547de99cdcc76ed2f5977371b3e7ef8d9dcc0e.zip |
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 'java/src')
-rw-r--r-- | java/src/main/java/com/google/protobuf/nano/MessageNanoPrinter.java | 2 | ||||
-rw-r--r-- | java/src/test/java/com/google/protobuf/NanoTest.java | 54 |
2 files changed, 53 insertions, 3 deletions
diff --git a/java/src/main/java/com/google/protobuf/nano/MessageNanoPrinter.java b/java/src/main/java/com/google/protobuf/nano/MessageNanoPrinter.java index d8734620..3a5ee7c1 100644 --- a/java/src/main/java/com/google/protobuf/nano/MessageNanoPrinter.java +++ b/java/src/main/java/com/google/protobuf/nano/MessageNanoPrinter.java @@ -105,7 +105,7 @@ public final class MessageNanoPrinter { if (arrayType == byte.class) { print(fieldName, fieldType, value, indentBuf, buf); } else { - int len = Array.getLength(value); + int len = value == null ? 0 : Array.getLength(value); for (int i = 0; i < len; i++) { Object elem = Array.get(value, i); print(fieldName, arrayType, elem, indentBuf, buf); diff --git a/java/src/test/java/com/google/protobuf/NanoTest.java b/java/src/test/java/com/google/protobuf/NanoTest.java index 4f2ac3f8..dbc0dddf 100644 --- a/java/src/test/java/com/google/protobuf/NanoTest.java +++ b/java/src/test/java/com/google/protobuf/NanoTest.java @@ -2677,13 +2677,63 @@ public class NanoTest extends TestCase { assertHasWireData(message, false); } + public void testNullRepeatedFields() throws Exception { + // Check that serialization after explicitly setting a repeated field + // to null doesn't NPE. + TestAllTypesNano message = new TestAllTypesNano(); + message.repeatedInt32 = null; + MessageNano.toByteArray(message); // should not NPE + message.toString(); // should not NPE + + message = new TestAllTypesNano(); + message.repeatedNestedEnum = null; + MessageNano.toByteArray(message); // should not NPE + message.toString(); // should not NPE + + message = new TestAllTypesNano(); + message.repeatedBytes = null; + MessageNano.toByteArray(message); // should not NPE + message.toString(); // should not NPE + + message = new TestAllTypesNano(); + message.repeatedNestedMessage = null; + MessageNano.toByteArray(message); // should not NPE + message.toString(); // should not NPE + + // Create a second message to merge into message. + TestAllTypesNano secondMessage = new TestAllTypesNano(); + TestAllTypesNano.NestedMessage nested = + new TestAllTypesNano.NestedMessage(); + nested.bb = 55; + secondMessage.repeatedNestedMessage = + new TestAllTypesNano.NestedMessage[] { nested }; + + // Should not NPE + message.mergeFrom(CodedInputByteBufferNano.newInstance( + MessageNano.toByteArray(secondMessage))); + assertEquals(55, message.repeatedNestedMessage[0].bb); + } + private void assertHasWireData(MessageNano message, boolean expected) { - int wireLength = MessageNano.toByteArray(message).length; + byte[] bytes = MessageNano.toByteArray(message); + int wireLength = bytes.length; if (expected) { assertFalse(wireLength == 0); } else { - assertEquals(0, wireLength); + if (wireLength != 0) { + fail("Expected no wire data for message \n" + message + + "\nBut got:\n" + + hexDump(bytes)); + } + } + } + + private static String hexDump(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + for (byte b : bytes) { + sb.append(String.format("%02x ", b)); } + return sb.toString(); } private <T> List<T> list(T first, T... remaining) { |