Skip to content

Commit 1e3f37b

Browse files
committed
feat(#877): add additional validation, test case
1 parent 7a8c216 commit 1e3f37b

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/main/java/org/json/JSONArray.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ private void parseTokener(JSONTokener x, JSONParserConfiguration jsonParserConfi
121121

122122
throwErrorIfEoF(x);
123123

124+
if(strictMode && cursor == ']'){
125+
throw x.syntaxError(getInvalidCharErrorMsg(cursor));
126+
}
127+
124128
if (cursor == ']') {
125129
break;
126130
}
@@ -135,7 +139,7 @@ private void parseTokener(JSONTokener x, JSONParserConfiguration jsonParserConfi
135139
boolean isNotEoF = !x.end();
136140

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

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

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

153157
this.myArrayList.add(x.nextValue(jsonParserConfiguration));
@@ -1954,6 +1958,7 @@ private void addAll(Object array, boolean wrap) throws JSONException {
19541958
private void addAll(Object array, boolean wrap, int recursionDepth) {
19551959
addAll(array, wrap, recursionDepth, new JSONParserConfiguration());
19561960
}
1961+
19571962
/**
19581963
* Add an array's elements to the JSONArray.
19591964
*`
@@ -2000,7 +2005,6 @@ private void addAll(Object array, boolean wrap, int recursionDepth, JSONParserCo
20002005
"JSONArray initial value should be a string or collection or array.");
20012006
}
20022007
}
2003-
20042008
/**
20052009
* Create a new JSONException in a common format for incorrect conversions.
20062010
* @param idx index of the item
@@ -2029,4 +2033,7 @@ private static JSONException wrongValueFormatException(
20292033
, cause);
20302034
}
20312035

2036+
private static String getInvalidCharErrorMsg(char cursor) {
2037+
return String.format("invalid character '%s' found after end of array", cursor);
2038+
}
20322039
}

src/test/java/org/json/junit/JSONParserConfigurationTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ public void verifyMaxDepthThenDuplicateKey() {
287287
private List<String> getNonCompliantJSONList() {
288288
return Arrays.asList(
289289
"[1],",
290+
"[1,]",
290291
"[[1]\"sa\",[2]]a",
291292
"[1],\"dsa\": \"test\"",
292293
"[[a]]",

0 commit comments

Comments
 (0)