fangyidong / json-simple

A simple Java toolkit for JSON. You can use json-simple to encode or decode JSON text.
Apache License 2.0
746 stars 340 forks source link

Bug: StackOverflow in toString() (infinite recursion) #148

Open kmnls opened 2 years ago

kmnls commented 2 years ago

Minimal reproducible example:

import org.json.simple.JSONObject;

public class Test {
    public static void main(String[] args) throws Exception {

        JSONObject obj1=new JSONObject();
        obj1.put("",obj1);
        obj1.toString();
     }
}

Description:

Converting invalid JSONObject to String will result in stack overflow.

Log:

Exception in thread "main" java.lang.StackOverflowError at org.json.simple.JSONObject.writeJSONString(JSONObject.java:55) at org.json.simple.JSONObject.writeJSONString(JSONObject.java:74) at org.json.simple.JSONValue.writeJSONString(JSONValue.java:160) at org.json.simple.JSONObject.writeJSONString(JSONObject.java:68) at org.json.simple.JSONObject.writeJSONString(JSONObject.java:74) at org.json.simple.JSONValue.writeJSONString(JSONValue.java:160) at org.json.simple.JSONObject.writeJSONString(JSONObject.java:68) at org.json.simple.JSONObject.writeJSONString(JSONObject.java:74) at org.json.simple.JSONValue.writeJSONString(JSONValue.java:160) at org.json.simple.JSONObject.writeJSONString(JSONObject.java:68) at org.json.simple.JSONObject.writeJSONString(JSONObject.java:74) at org.json.simple.JSONValue.writeJSONString(JSONValue.java:160) at org.json.simple.JSONObject.writeJSONString(JSONObject.java:68)