Return the default instance when parsing from an empty byte[].

PiperOrigin-RevId: 631213831
pull/16698/head
Protobuf Team Bot 11 months ago committed by Copybara-Service
parent 55592a28e7
commit f597991857
  1. 11
      java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java
  2. 8
      java/lite/src/test/java/com/google/protobuf/LiteTest.java

@ -1621,10 +1621,17 @@ public abstract class GeneratedMessageLite<
/** 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(

@ -2423,6 +2423,12 @@ public class LiteTest {
int unused = TestRecursiveOneof.getDefaultInstance().hashCode();
}
@Test
public void testParseFromEmptyBytes() throws Exception {
assertThat(TestAllTypesLite.parseFrom(new byte[] {}))
.isSameInstanceAs(TestAllTypesLite.getDefaultInstance());
}
@Test
public void testParseFromByteBuffer() throws Exception {
TestAllTypesLite message =
@ -2718,7 +2724,7 @@ public class LiteTest {
@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) {
}

Loading…
Cancel
Save