Skip to content

Cycle detection doesn't catch cycles through collections #652

@cushon

Description

@cushon

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)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions