JSON-java icon indicating copy to clipboard operation
JSON-java copied to clipboard

Cycle detection doesn't catch cycles through collections

Open cushon opened this issue 4 years ago • 3 comments

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)

cushon avatar Nov 27 '21 01:11 cushon

To be fixed

stleary avatar Dec 01 '21 03:12 stleary

@stleary this is a different issue than the one fixed in #651

johnjaylward avatar Dec 01 '21 03:12 johnjaylward

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.

hendrixjoseph avatar Oct 20 '22 01:10 hendrixjoseph

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.

stleary avatar Sep 04 '23 18:09 stleary