JSON-java
JSON-java copied to clipboard
Cycle detection doesn't catch cycles through collections
The cycle detection introduced in #632 / #645 doesn't handle cycles that involved collections. The following example still results in a StackOverflowError:
import java.util.Collections;
import java.util.List;
import org.json.JSONObject;
class Json {
public static void main(String[] args) {
new JSONObject(new Foo());
}
public static class Foo {
public List<Foo> getFoos() {
return Collections.singletonList(this);
}
}
}
Exception in thread "main" java.lang.StackOverflowError
at org.json.JSONObject.getKeyNameFromMethod(JSONObject.java:1591)
at org.json.JSONObject.populateMap(JSONObject.java:1548)
at org.json.JSONObject.populateMap(JSONObject.java:1529)
at org.json.JSONObject.<init>(JSONObject.java:366)
at org.json.JSONObject.wrap(JSONObject.java:2499)
at org.json.JSONObject.wrap(JSONObject.java:2457)
at org.json.JSONArray.addAll(JSONArray.java:1609)
at org.json.JSONArray.<init>(JSONArray.java:176)
at org.json.JSONObject.wrap(JSONObject.java:2478)
at org.json.JSONObject.populateMap(JSONObject.java:1563)
at org.json.JSONObject.populateMap(JSONObject.java:1529)
at org.json.JSONObject.<init>(JSONObject.java:366)
To be fixed
@stleary this is a different issue than the one fixed in #651
I have a fix for this (plus a unit test), as well as a couple other locations this might still be happening. I'll try to make a PR tomorrow.
Can you assign me please? Thank you.
I don't think it will be possible to check for cycles in collections without impacting performance. If you disagree, post here to reopen this ticket.