diff options
author | Jan-Willem Maarse <maarse@google.com> | 2014-05-01 09:59:09 -0700 |
---|---|---|
committer | Jan-Willem Maarse <maarse@google.com> | 2014-05-01 11:08:38 -0700 |
commit | 1ee2760aed05bf072a05fd7b6aeb158691a5dfbc (patch) | |
tree | 4070a6563d0d3f3637a8f5dc6677e38da93f84ce | |
parent | 33a6680ab98977cd5b9b7b03f4875b9265cfec3b (diff) | |
download | external_protobuf-1ee2760aed05bf072a05fd7b6aeb158691a5dfbc.zip external_protobuf-1ee2760aed05bf072a05fd7b6aeb158691a5dfbc.tar.gz external_protobuf-1ee2760aed05bf072a05fd7b6aeb158691a5dfbc.tar.bz2 |
Fix NPE when clearing an extension in nano protos
If ExtendableMessageNano doesn't have any unknown fields, trying to
clear an extension by setting it to null would throw an NPE.
Change-Id: I6abcdfcc0193de44f97b21dd6cc2f40604938a1a
-rw-r--r-- | java/src/main/java/com/google/protobuf/nano/Extension.java | 2 | ||||
-rw-r--r-- | java/src/test/java/com/google/protobuf/NanoTest.java | 14 |
2 files changed, 15 insertions, 1 deletions
diff --git a/java/src/main/java/com/google/protobuf/nano/Extension.java b/java/src/main/java/com/google/protobuf/nano/Extension.java index 177a9cc..dfe4f87 100644 --- a/java/src/main/java/com/google/protobuf/nano/Extension.java +++ b/java/src/main/java/com/google/protobuf/nano/Extension.java @@ -268,7 +268,7 @@ public class Extension<M extends ExtendableMessageNano<M>, T> { // After deletion or no-op addition (due to 'value' being an array of empty or // null-only elements), unknownFields may be empty. Discard the ArrayList if so. - return (unknownFields.size() == 0) ? null : unknownFields; + return (unknownFields == null || unknownFields.isEmpty()) ? null : unknownFields; } protected UnknownFieldData writeData(Object value) { diff --git a/java/src/test/java/com/google/protobuf/NanoTest.java b/java/src/test/java/com/google/protobuf/NanoTest.java index 5f28647..00e2597 100644 --- a/java/src/test/java/com/google/protobuf/NanoTest.java +++ b/java/src/test/java/com/google/protobuf/NanoTest.java @@ -2910,6 +2910,20 @@ public class NanoTest extends TestCase { assertTrue(Arrays.equals(enums, message.getExtension(RepeatedExtensions.repeatedEnum))); } + public void testNullExtensions() throws Exception { + // Check that clearing the extension on an empty message is a no-op. + Extensions.ExtendableMessage message = new Extensions.ExtendableMessage(); + message.setExtension(SingularExtensions.someMessage, null); + assertEquals(0, MessageNano.toByteArray(message).length); + + // Check that the message is empty after setting and clearing an extension. + AnotherMessage another = new AnotherMessage(); + message.setExtension(SingularExtensions.someMessage, another); + assertTrue(MessageNano.toByteArray(message).length > 0); + message.setExtension(SingularExtensions.someMessage, null); + assertEquals(0, MessageNano.toByteArray(message).length); + } + public void testUnknownFields() throws Exception { // Check that we roundtrip (serialize and deserialize) unrecognized fields. AnotherMessage message = new AnotherMessage(); |