mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-02 11:05:28 -04:00
fix(#887): double array breaking JSONTokener.nextValue
change(#887): input validation
This commit is contained in:
parent
ce074e9f9a
commit
3dcd5b2fab
@ -133,6 +133,17 @@ public class JSONArray implements Iterable<Object> {
|
||||
case ']':
|
||||
if (jsonParserConfiguration.isStrictMode()) {
|
||||
nextChar = x.nextClean();
|
||||
|
||||
if (nextChar == ','){
|
||||
x.back();
|
||||
return;
|
||||
}
|
||||
|
||||
if (nextChar == ']'){
|
||||
x.back();
|
||||
return;
|
||||
}
|
||||
|
||||
if (nextChar != 0) {
|
||||
throw x.syntaxError("invalid character found after end of array: " + nextChar);
|
||||
}
|
||||
@ -161,27 +172,14 @@ public class JSONArray implements Iterable<Object> {
|
||||
char cursor = x.getPrevious();
|
||||
|
||||
boolean isEndOfArray = cursor == ']';
|
||||
boolean nextCharacterIsNotEoF = x.nextClean() != 0;
|
||||
char nextChar = x.nextClean();
|
||||
boolean nextCharacterIsNotEoF = nextChar != 0;
|
||||
|
||||
if (isEndOfArray && nextCharacterIsNotEoF) {
|
||||
String completeInput = collectCompleteInput(x);
|
||||
throw new JSONException("Provided Array is not compliant with strict mode guidelines: " + completeInput);
|
||||
throw x.syntaxError(String.format("Provided Array is not compliant with strict mode guidelines: '%s'", nextChar));
|
||||
}
|
||||
}
|
||||
|
||||
private String collectCompleteInput(JSONTokener x) {
|
||||
String nonCompliantStringAfterArray = collectNonCompliantStringAfterArray(x);
|
||||
return myArrayList + nonCompliantStringAfterArray;
|
||||
}
|
||||
|
||||
private String collectNonCompliantStringAfterArray(JSONTokener x) {
|
||||
StringBuilder sb = new StringBuilder().append(x.getPrevious());
|
||||
while(x.nextClean() != 0){
|
||||
sb.append(x.getPrevious());
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a JSONArray from a source JSON text.
|
||||
*
|
||||
|
@ -440,7 +440,7 @@ public class JSONTokener {
|
||||
case '[':
|
||||
this.back();
|
||||
try {
|
||||
return new JSONArray(this);
|
||||
return new JSONArray(this, jsonParserConfiguration);
|
||||
} catch (StackOverflowError e) {
|
||||
throw new JSONException("JSON Array or Object depth too large to process.", e);
|
||||
}
|
||||
@ -516,6 +516,10 @@ public class JSONTokener {
|
||||
|
||||
String string = sb.toString().trim();
|
||||
|
||||
if (string.isEmpty()) {
|
||||
throw this.syntaxError("Missing value");
|
||||
}
|
||||
|
||||
if (strictMode) {
|
||||
boolean isBooleanOrNumeric = checkIfValueIsBooleanOrNumeric(string);
|
||||
|
||||
@ -526,9 +530,6 @@ public class JSONTokener {
|
||||
throw new JSONException(String.format("Value is not surrounded by quotes: %s", string));
|
||||
}
|
||||
|
||||
if (string.isEmpty()) {
|
||||
throw this.syntaxError("Missing value");
|
||||
}
|
||||
return JSONObject.stringToValue(string);
|
||||
}
|
||||
|
||||
|
@ -218,6 +218,7 @@ public class JSONParserConfigurationTest {
|
||||
*/
|
||||
private List<String> getNonCompliantJSONList() {
|
||||
return Arrays.asList(
|
||||
"[[a]]",
|
||||
"[]asdf",
|
||||
"[]]",
|
||||
"[]}",
|
||||
|
Loading…
x
Reference in New Issue
Block a user