Skip to content

Commit

Permalink
feat(#877): add additional validation, test case
Browse files Browse the repository at this point in the history
  • Loading branch information
rikkarth committed Apr 27, 2024
1 parent 7a8c216 commit 1e3f37b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/main/java/org/json/JSONArray.java
Expand Up @@ -121,6 +121,10 @@ private void parseTokener(JSONTokener x, JSONParserConfiguration jsonParserConfi

throwErrorIfEoF(x);

if(strictMode && cursor == ']'){
throw x.syntaxError(getInvalidCharErrorMsg(cursor));
}

if (cursor == ']') {
break;
}
Expand All @@ -135,7 +139,7 @@ private void parseTokener(JSONTokener x, JSONParserConfiguration jsonParserConfi
boolean isNotEoF = !x.end();

if (isNotEoF && x.getArrayLevel() == 0) {
throw x.syntaxError(String.format("invalid character '%s' found after end of array", cursor));
throw x.syntaxError(getInvalidCharErrorMsg(cursor));
}

x.back();
Expand All @@ -147,7 +151,7 @@ private void parseTokener(JSONTokener x, JSONParserConfiguration jsonParserConfi
boolean quoteIsNotNextToValidChar = x.getPreviousChar() != ',' && x.getPreviousChar() != '[';

if (strictMode && currentCharIsQuote && quoteIsNotNextToValidChar) {
throw x.syntaxError(String.format("invalid character '%s' found after end of array", cursor));
throw x.syntaxError(getInvalidCharErrorMsg(cursor));
}

this.myArrayList.add(x.nextValue(jsonParserConfiguration));
Expand Down Expand Up @@ -1954,6 +1958,7 @@ private void addAll(Object array, boolean wrap) throws JSONException {
private void addAll(Object array, boolean wrap, int recursionDepth) {
addAll(array, wrap, recursionDepth, new JSONParserConfiguration());
}

/**
* Add an array's elements to the JSONArray.
*`
Expand Down Expand Up @@ -2000,7 +2005,6 @@ private void addAll(Object array, boolean wrap, int recursionDepth, JSONParserCo
"JSONArray initial value should be a string or collection or array.");
}
}

/**
* Create a new JSONException in a common format for incorrect conversions.
* @param idx index of the item
Expand Down Expand Up @@ -2029,4 +2033,7 @@ private static JSONException wrongValueFormatException(
, cause);
}

private static String getInvalidCharErrorMsg(char cursor) {
return String.format("invalid character '%s' found after end of array", cursor);
}
}
Expand Up @@ -287,6 +287,7 @@ public void verifyMaxDepthThenDuplicateKey() {
private List<String> getNonCompliantJSONList() {
return Arrays.asList(
"[1],",
"[1,]",
"[[1]\"sa\",[2]]a",
"[1],\"dsa\": \"test\"",
"[[a]]",
Expand Down

0 comments on commit 1e3f37b

Please sign in to comment.