Skip to content

Commit

Permalink
#829 Throw KryoException instead of IndexOutOfBoundsException whe…
Browse files Browse the repository at this point in the history
…n reference cannot be resolved
  • Loading branch information
theigl committed May 23, 2021
1 parent 2b0a2c5 commit a830dac
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/com/esotericsoftware/kryo/Kryo.java
Expand Up @@ -919,7 +919,11 @@ int readReferenceOrNull (Input input, Class type, boolean mayBeNull) {
}
// The id is an object reference.
id -= 2; // - 2 because 0 and 1 are used for NULL and NOT_NULL.
readObject = referenceResolver.getReadObject(type, id);
try {
readObject = referenceResolver.getReadObject(type, id);
} catch (Exception e) {
throw new KryoException("Unable to resolve reference for " + className(type) + " with id: " + id, e);
}
if (DEBUG) debug("kryo", "Read reference " + id + ": " + string(readObject) + pos(input.position()));
return REF;
}
Expand Down
14 changes: 14 additions & 0 deletions test/com/esotericsoftware/kryo/ReferenceTest.java
Expand Up @@ -104,6 +104,20 @@ void testReadingNestedObjectsFirst () {
roundTrip(23, subList);
}

@Test
void testReadInvalidReference() {
kryo.setReferences(true);
kryo.register(Ordering.class);
final Input input = new Input(new byte[]{3});
try {
kryo.readObject(input, Ordering.class);
} catch (KryoException ex) {
assertTrue(ex.getMessage().contains("Unable to resolve reference"));
return;
}
fail("Exception was expected");
}

public static class SubListSerializer extends Serializer<List> {
private Field listField, offsetField, sizeField;

Expand Down

0 comments on commit a830dac

Please sign in to comment.