Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSONObject stack overflow #837

Open
Renis-1 opened this issue Dec 7, 2023 · 4 comments
Open

JSONObject stack overflow #837

Renis-1 opened this issue Dec 7, 2023 · 4 comments

Comments

@Renis-1
Copy link

Renis-1 commented Dec 7, 2023

If a list has itself as an element JSONObject(list) causes stack overflow error as seen in following code:

List<List<?>> list = new LinkedList<>();
list.add(list);
JSONObject j = new JSONObject(list);
Repository owner deleted a comment from Feiys Dec 9, 2023
@stleary
Copy link
Owner

stleary commented Dec 9, 2023

@Renis-1 Thanks for bringing this up. Will see if a fix can be added after #823 is committed.

@michalbbednarczyk
Copy link

stack overflow error is also caused by:

Deque<Deque<?>> deque = new ArrayDeque<>();
deque.add(deque);
JSONObject jo = new JSONObject(deque);
BlockingDeque<BlockingDeque<?>> deque = new LinkedBlockingDeque<>();
deque.add(deque);
JSONObject jo = new JSONObject(deque);
Properties properties = new Properties();
properties.put("key", properties);
JSONObject jo = new JSONObject(properties);
EnumMap<TestEnum, EnumMap<TestEnum, ?>> map = new EnumMap<>(TestEnum.class);
map.put(TestEnum.CONSTANT1, map);
JSONObject jo = new JSONObject(map);
Hashtable<String, Hashtable<String, ?>> table = new Hashtable<>();
table.put("key", table);
JSONObject jo = new JSONObject(table);
Map<String, Map<String, ?>> map = new IdentityHashMap<>();
map.put("key", map);
JSONObject jo = new JSONObject(map);
Map<String, Map<String, ?>> map = new TreeMap<>();
map.put("key", map);
JSONObject jo = new JSONObject(map);
Map<String, Map<String, ?>> map = new WeakHashMap<>();
map.put("key", map);
JSONObject jo = new JSONObject(map);
Map<String, Map<String, ?>> map = new LinkedHashMap<>();
map.put("key", map);
JSONObject jo = new JSONObject(map);
SortedMap<String, SortedMap<String, ?>> map = new TreeMap<>();
map.put("key", map);
JSONObject jo = new JSONObject(map);
NavigableMap<String, NavigableMap<String, ?>> map = new TreeMap<>();
map.put("key", map);
JSONObject jo = new JSONObject(map);
Map<String, Map<String, ?>> map = new ConcurrentHashMap<>();
map.put("key", map);
JSONObject jo = new JSONObject(map);
Queue<Stack<?>> queue = new LinkedList<>();
Stack<Stack<?>> stack = new Stack<>();
stack.add(stack);
queue.add(stack);
JSONObject jo = new JSONObject(queue);
Deque<Deque<?>> deque = new LinkedBlockingDeque<>();
deque.add(deque);
JSONObject jo = new JSONObject(deque);
Queue<Runnable> queue = new SynchronousQueue<>();
Runnable element = () -> {};
queue.add(element);
JSONObject jo = new JSONObject(queue);
Deque<Deque<?>> deque = new ConcurrentLinkedDeque<>();
deque.add(deque);
JSONObject jo = new JSONObject(deque);
ConcurrentSkipListMap<String, ConcurrentSkipListMap<String, ?>> map = new ConcurrentSkipListMap<>();
map.put("key", map);
JSONObject jo = new JSONObject(map);

@johnjaylward
Copy link
Contributor

Yeah... don't do that. The JavaDoc specifically states that that will happen and not to use cyclical references.

@stleary
Copy link
Owner

stleary commented Jan 13, 2024

@micbedn Thanks for posting. Are you sure you are testing with the latest code, though? I am only seeing JSONExceptions:

    @Test
    public void foo() {
        Deque<Deque<?>> deque = new ArrayDeque<>();
        deque.add(deque);
        JSONObject jo = new JSONObject(deque);
    }

Output:

JSONArray has reached recursion depth limit of 512
org.json.JSONException: JSONArray has reached recursion depth limit of 512

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants