This is an automated email from the ASF dual-hosted git repository. chaokunyang pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/fory.git
The following commit(s) were added to refs/heads/main by this push: new 9eccb2ce3 chore(Java): handle deserialization scenario that currently throws an IndexOutOfBoundsException (#2399) 9eccb2ce3 is described below commit 9eccb2ce3a3324fee53859fc77809bd18feab882 Author: PJ Fanning <pjfann...@users.noreply.github.com> AuthorDate: Wed Jul 9 18:18:36 2025 +0100 chore(Java): handle deserialization scenario that currently throws an IndexOutOfBoundsException (#2399) This test fails. I had expected it to fail with null when wrong type was used (because of #2392 but this fails differently from that - the use case where I saw a null returned was a Scala use case) Fails with IndexOutOfBoundsException while I would expect a nicer exception. Maybe should be a Fory DeserializationException. ``` java.lang.IndexOutOfBoundsException: readerIndex(1) + length(2) exceeds size(7): org.apache.fory.memory.MemoryBuffer$BoundChecker@4e268090 at org.apache.fory.memory.MemoryBuffer$BoundChecker.fillBuffer(MemoryBuffer.java:186) at org.apache.fory.memory.MemoryBuffer.checkReadableBytes(MemoryBuffer.java:2440) at org.apache.fory.ForyTest_Struct2ForyCodec_0.read(ForyTest_Struct2ForyCodec_0.java:59) at org.apache.fory.Fory.readDataInternal(Fory.java:1055) at org.apache.fory.Fory.deserializeJavaObject(Fory.java:1223) at org.apache.fory.Fory.deserializeJavaObject(Fory.java:1200) at org.apache.fory.ForyTest.testDeserializeJavaObjectWrongType(ForyTest.java:689) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ``` --- .../main/java/org/apache/fory/util/ExceptionUtils.java | 3 +++ .../src/test/java/org/apache/fory/ForyTest.java | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/java/fory-core/src/main/java/org/apache/fory/util/ExceptionUtils.java b/java/fory-core/src/main/java/org/apache/fory/util/ExceptionUtils.java index dd6399234..51adcd671 100644 --- a/java/fory-core/src/main/java/org/apache/fory/util/ExceptionUtils.java +++ b/java/fory-core/src/main/java/org/apache/fory/util/ExceptionUtils.java @@ -25,6 +25,7 @@ import java.util.List; import org.apache.fory.Fory; import org.apache.fory.collection.ObjectArray; import org.apache.fory.exception.DeserializationException; +import org.apache.fory.exception.ForyException; import org.apache.fory.memory.Platform; import org.apache.fory.reflect.ReflectionUtils; import org.apache.fory.resolver.MapRefResolver; @@ -61,6 +62,8 @@ public class ExceptionUtils { // carry with read objects for better trouble shooting. List<Object> objects = Arrays.asList(readObjects.objects).subList(0, readObjects.size); throw new DeserializationException(objects, t); + } else if (t instanceof Exception && !(t instanceof ForyException)) { + throw new DeserializationException("Failed to deserialize input", t); } else { Platform.throwException(t); throw new IllegalStateException("unreachable"); diff --git a/java/fory-core/src/test/java/org/apache/fory/ForyTest.java b/java/fory-core/src/test/java/org/apache/fory/ForyTest.java index 4d8a10a75..335cc9720 100644 --- a/java/fory-core/src/test/java/org/apache/fory/ForyTest.java +++ b/java/fory-core/src/test/java/org/apache/fory/ForyTest.java @@ -27,7 +27,7 @@ import static org.testng.Assert.assertTrue; import com.google.common.collect.HashBasedTable; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; -import java.io.*; +import java.io.Serializable; import java.lang.invoke.MethodHandles; import java.math.BigDecimal; import java.math.BigInteger; @@ -58,6 +58,7 @@ import org.apache.fory.builder.Generated; import org.apache.fory.config.CompatibleMode; import org.apache.fory.config.ForyBuilder; import org.apache.fory.config.Language; +import org.apache.fory.exception.DeserializationException; import org.apache.fory.exception.ForyException; import org.apache.fory.exception.InsecureException; import org.apache.fory.memory.MemoryBuffer; @@ -641,6 +642,7 @@ public class ForyTest extends ForyTestBase { assertEquals(fory.getBuffer().size(), limitInBytes); } + @EqualsAndHashCode static class Struct1 { int f1; String f2; @@ -673,4 +675,16 @@ public class ForyTest extends ForyTestBase { Assert.assertEquals(struct1.f1, struct2.f1); Assert.assertEquals(struct1.f2, struct2.f2); } + + @Test + public void testDeserializeJavaObjectWrongType() { + Fory fory = Fory.builder().requireClassRegistration(false).build(); + Struct1 struct1 = new Struct1(10, "abc"); + byte[] bytes = fory.serializeJavaObject(struct1); + // first deserialize as Struct1 (correct type) + Assert.assertEquals(fory.deserializeJavaObject(bytes, Struct1.class), struct1); + // then deserialize as Struct2 (wrong type) + Assert.assertThrows( + DeserializationException.class, () -> fory.deserializeJavaObject(bytes, Struct2.class)); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@fory.apache.org For additional commands, e-mail: commits-h...@fory.apache.org