Skip to content

Commit 4afc56c

Browse files
committed
#789 Catch StackOverflowError and add hint for enabling references
1 parent ae777c6 commit 4afc56c

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/com/esotericsoftware/kryo/serializers/ReflectField.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ public void write (Output output, Object object) {
9292
} catch (KryoException ex) {
9393
ex.addTrace(name + " (" + object.getClass().getName() + ")");
9494
throw ex;
95+
} catch (StackOverflowError ex) {
96+
throw new KryoException(
97+
"A StackOverflow occurred. The most likely cause is that your data has a circular reference resulting in " +
98+
"infinite recursion. Try enabling references with Kryo.setReferences(true). If your data structure " +
99+
"is really more than " + kryo.getDepth() + " levels deep then try increasing your Java stack size.",
100+
ex);
95101
} catch (Throwable t) {
96102
KryoException ex = new KryoException(t);
97103
ex.addTrace(name + " (" + object.getClass().getName() + ")");

test/com/esotericsoftware/kryo/serializers/FieldSerializerTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,22 @@ public void testDeep () {
705705
roundTrip(1440, root);
706706
}
707707

708+
@Test
709+
public void testCircularReference () {
710+
kryo.register(CircularReference.class);
711+
kryo.register(CircularReference.Inner.class);
712+
713+
CircularReference instance = new CircularReference();
714+
try {
715+
roundTrip(1, instance);
716+
} catch (KryoException ex) {
717+
assertTrue(ex.getMessage().contains("Infinite recursion (StackOverflowError)."));
718+
return;
719+
}
720+
721+
fail("Exception was expected");
722+
}
723+
708724
public static class DefaultTypes {
709725
// Primitives.
710726
public boolean booleanField;
@@ -1279,4 +1295,17 @@ public boolean equals (Object obj) {
12791295
return true;
12801296
}
12811297
}
1298+
1299+
static class CircularReference {
1300+
Inner b = new Inner(this);
1301+
1302+
static class Inner {
1303+
CircularReference a;
1304+
1305+
public Inner(CircularReference a) {
1306+
this.a = a;
1307+
}
1308+
}
1309+
}
1310+
12821311
}

0 commit comments

Comments
 (0)