Return the default instance when parsing from an empty byte[]. PiperOrigin-RevId: 631213831
diff --git a/java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java b/java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java index 115f428..0937114 100644 --- a/java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java +++ b/java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java
@@ -1621,10 +1621,17 @@ /** A static helper method for parsing a partial from byte array. */ private static <T extends GeneratedMessageLite<T, ?>> T parsePartialFrom( - T instance, byte[] input, int offset, int length, ExtensionRegistryLite extensionRegistry) + T defaultInstance, + byte[] input, + int offset, + int length, + ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException { + if (length == 0) { + return defaultInstance; + } @SuppressWarnings("unchecked") // Guaranteed by protoc - T result = instance.newMutableInstance(); + T result = defaultInstance.newMutableInstance(); try { Schema<T> schema = Protobuf.getInstance().schemaFor(result); schema.mergeFrom(
diff --git a/java/lite/src/test/java/com/google/protobuf/LiteTest.java b/java/lite/src/test/java/com/google/protobuf/LiteTest.java index 754ed7d..0c7b8b5 100644 --- a/java/lite/src/test/java/com/google/protobuf/LiteTest.java +++ b/java/lite/src/test/java/com/google/protobuf/LiteTest.java
@@ -2424,6 +2424,12 @@ } @Test + public void testParseFromEmptyBytes() throws Exception { + assertThat(TestAllTypesLite.parseFrom(new byte[] {})) + .isSameInstanceAs(TestAllTypesLite.getDefaultInstance()); + } + + @Test public void testParseFromByteBuffer() throws Exception { TestAllTypesLite message = TestAllTypesLite.newBuilder() @@ -2718,7 +2724,7 @@ @Test public void testNullExtensionRegistry() throws Exception { try { - TestAllTypesLite.parseFrom(new byte[] {}, null); + TestAllTypesLite.parseFrom(TestUtilLite.getAllLiteSetBuilder().build().toByteArray(), null); assertWithMessage("expected exception").fail(); } catch (NullPointerException expected) { }