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

Fixing issue 60 #62

Merged
merged 1 commit into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/main/java/org/codehaus/jettison/json/JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,22 +182,30 @@ public JSONArray(String string) throws JSONException {
* @throws JSONException If there is a syntax error.
*/
public JSONArray(Collection collection) throws JSONException {
this(collection, 0);
}

private JSONArray(Collection collection, int recursionDepth) throws JSONException {
if (recursionDepth > JSONObject.getGlobalRecursionDepthLimit()) {
throw new JSONException("JSONArray has reached recursion depth limit of "
+ JSONObject.getGlobalRecursionDepthLimit());
}

this.myArrayList = (collection == null) ?
new ArrayList() :
new ArrayList(collection);
// ensure a pure hierarchy of JSONObjects and JSONArrays
for (ListIterator iter = myArrayList.listIterator(); iter.hasNext();) {
Object e = iter.next();
if (e instanceof Collection) {
iter.set(new JSONArray((Collection) e));
}
if (e instanceof Map) {
iter.set(new JSONObject((Map) e));
}
}
Object e = iter.next();
if (e instanceof Collection) {
iter.set(new JSONArray((Collection) e, recursionDepth + 1));
}
if (e instanceof Map) {
iter.set(new JSONObject((Map) e));
}
}
}


/**
* Get the object value associated with an index.
* @param index
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/codehaus/jettison/json/JSONArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import junit.framework.TestCase;

import java.util.ArrayList;
import java.util.List;

public class JSONArrayTest extends TestCase {
public void testInvalidArraySequence() throws Exception {
try {
Expand Down Expand Up @@ -67,6 +70,18 @@ public void testInfiniteLoop2() {
public void testIssue52() throws JSONException {
JSONObject.setGlobalRecursionDepthLimit(10);
new JSONArray("[{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {a:10}]");
JSONObject.setGlobalRecursionDepthLimit(500);
}

// https://github.com/jettison-json/jettison/issues/60
public void testIssue60() throws JSONException {
List<Object> list = new ArrayList<>();
list.add(list);
try {
new JSONArray(list);
} catch (JSONException ex) {
assertEquals(ex.getMessage(), "JSONArray has reached recursion depth limit of 500");
}
}

}