[JAVA-174] prevent stack overflow in case of erroneous cyclic reference Created: 06/Oct/10  Updated: 25/Jun/13  Resolved: 15/Feb/11

Status: Closed
Project: Java Driver
Component/s: Codecs
Affects Version/s: 2.1
Fix Version/s: 2.5

Type: Improvement Priority: Major - P3
Reporter: Ilya Brodotsky Assignee: Antoine Girbal
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Synthetic example, but possible real case:

Domain class
---------------------------------------------

public class E3 extends ReflectionDBObject {
private String stringData;
private E4 e4data;

public E3() {
}

public E3(String stringData, E4 e4data)

{ this.stringData = stringData; this.e4data = e4data; }

public String getStringData()

{ return stringData; }

public void setStringData(String stringData)

{ this.stringData = stringData; }

public E4 getE4data()

{ return e4data; }

public void setE4data(E4 e4data)

{ this.e4data = e4data; }

}

Domain class
---------------------------------------------

public class E4 extends ReflectionDBObject {
private int integerData;
private E3 e3data;

public E4() {
}

public E4(int integerData, E3 e3data)

{ this.integerData = integerData; this.e3data = e3data; }

public int getIntegerData()

{ return integerData; }

public void setIntegerData(int integerData)

{ this.integerData = integerData; }

public E3 getE3data()

{ return e3data; }

public void setE3data(E3 e3data)

{ this.e3data = e3data; }

}

Application code
---------------------------------------------

E3 e3 = new E3();
E4 e4 = new E4(4, e3);
e3.setE4data(e4);
e3.setStringData("e3");

coll.save(e3);

---------------------------------------------

Exception in thread "main" java.lang.StackOverflowError
...
at com.mongodb.ReflectionDBObject$JavaWrapper.get(ReflectionDBObject.java:155)
at com.mongodb.ReflectionDBObject.get(ReflectionDBObject.java:31)
at com.mongodb.DBCollection._checkKeys(DBCollection.java:852)
at com.mongodb.DBCollection._checkKeys(DBCollection.java:853)
at com.mongodb.DBCollection._checkKeys(DBCollection.java:853)
...



 Comments   
Comment by Antoine Girbal [ 15/Feb/11 ]

thanks for reporting issue.
Fixing it would require keeping a hashmap of already checked objects.
This would clutter the code and slow down operations.
The case you describe should be a fail-fast, meaning that the error will happen right away when cyclical references are inserted, and the user will be able to work around it during dev phase.
I dont think this needs a fix.

Generated at Thu Feb 08 08:51:40 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.