Merge pull request #1074 from XIAYM-gh/master

Fix strict mode failure when JSONTokener.next() and back() are called before parsing
This commit is contained in:
Sean Leary
2026-09-13 07:57:15 -05:00
committed by GitHub
5 changed files with 141 additions and 9 deletions
+14 -2
View File
@@ -80,7 +80,7 @@ public class JSONArray implements Iterable<Object> {
* @param x
* A JSONTokener
* @throws JSONException
* If there is a syntax error.
* If there is a syntax error.
*/
public JSONArray(JSONTokener x) throws JSONException {
this(x, x.getJsonParserConfiguration());
@@ -94,9 +94,21 @@ public class JSONArray implements Iterable<Object> {
* @throws JSONException If a syntax error occurs during the construction of the JSONArray.
*/
public JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
this(x, jsonParserConfiguration, true);
}
/**
* Constructs a JSONArray from a JSONTokener and a JSONParserConfiguration, for internal use. <br>
* Never call this instead of using withStrictMode(boolean).
*
* @param x A JSONTokener instance from which the JSONArray is constructed.
* @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
* @param isInitial A boolean that determines whether this array is the root.
* @throws JSONException If a syntax error occurs during the construction of the JSONArray.
*/
JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) throws JSONException {
this();
boolean isInitial = x.getPrevious() == 0;
if (x.nextClean() != '[') {
throw x.syntaxError("A JSONArray text must start with '['");
}
+22 -5
View File
@@ -195,8 +195,8 @@ public class JSONObject {
* @param x
* A JSONTokener object containing the source string.
* @throws JSONException
* If there is a syntax error in the source string or a
* duplicated key.
* If there is a syntax error in the source string or a
* duplicated key.
*/
public JSONObject(JSONTokener x) throws JSONException {
this(x, x.getJsonParserConfiguration());
@@ -210,12 +210,29 @@ public class JSONObject {
* @param jsonParserConfiguration
* Variable to pass parser custom configuration for json parsing.
* @throws JSONException
* If there is a syntax error in the source string or a
* duplicated key.
* If there is a syntax error in the source string or a
* duplicated key.
*/
public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
this(x, jsonParserConfiguration, true);
}
/**
* Construct a JSONObject from a JSONTokener with custom json parse configurations, for internal use. <br>
* Never call this instead of using withStrictMode(boolean).
*
* @param x
* A JSONTokener object containing the source string.
* @param jsonParserConfiguration
* Variable to pass parser custom configuration for json parsing.
* @param isInitial
* A boolean that determines whether this object is the root.
* @throws JSONException
* If there is a syntax error in the source string or a
* duplicated key.
*/
JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) throws JSONException {
this();
boolean isInitial = x.getPrevious() == 0;
if (x.nextClean() != '{') {
throw x.syntaxError("A JSONObject text must begin with '{'");
+2 -2
View File
@@ -458,14 +458,14 @@ public class JSONTokener {
case '{':
this.back();
try {
return new JSONObject(this, jsonParserConfiguration);
return new JSONObject(this, jsonParserConfiguration, false);
} catch (StackOverflowError e) {
throw new JSONException("JSON Array or Object depth too large to process.", e);
}
case '[':
this.back();
try {
return new JSONArray(this, jsonParserConfiguration);
return new JSONArray(this, jsonParserConfiguration, false);
} catch (StackOverflowError e) {
throw new JSONException("JSON Array or Object depth too large to process.", e);
}
@@ -1566,4 +1566,56 @@ public class JSONArrayTest {
"[1,null,3]", jsonArray.toString());
}
}
@Test
public void strictModeShouldCheckTrailingCharactersAfterNextAndBack() {
JSONParserConfiguration strict =
new JSONParserConfiguration().withStrictMode();
JSONTokener tok = new JSONTokener("[]xxx");
tok.next();
tok.back();
JSONException exception = assertThrows(
JSONException.class,
() -> new JSONArray(tok, strict));
assertTrue(exception.getMessage().contains(
"Unparsed characters found at end of input text"));
}
@Test
public void strictModeShouldCheckTrailingCharactersAfterConsumedWhitespace() {
JSONParserConfiguration strict =
new JSONParserConfiguration().withStrictMode();
JSONTokener tok = new JSONTokener(" []xxx");
tok.next();
tok.next();
tok.back();
JSONException exception = assertThrows(
JSONException.class,
() -> new JSONArray(tok, strict));
assertTrue(exception.getMessage().contains(
"Unparsed characters found at end of input text"));
}
@Test
public void strictModeShouldCheckTrailingCharactersAfterNextCleanAndBack() {
JSONParserConfiguration strict =
new JSONParserConfiguration().withStrictMode();
JSONTokener tok = new JSONTokener(" []xxx");
tok.nextClean();
tok.back();
JSONException exception = assertThrows(
JSONException.class,
() -> new JSONArray(tok, strict));
assertTrue(exception.getMessage().contains(
"Unparsed characters found at end of input text"));
}
}
@@ -4337,4 +4337,55 @@ public class JSONObjectTest {
}
}
@Test
public void strictModeShouldCheckTrailingCharactersAfterNextAndBack() {
JSONParserConfiguration strict =
new JSONParserConfiguration().withStrictMode();
JSONTokener tok = new JSONTokener("{}xxx");
tok.next();
tok.back();
JSONException exception = assertThrows(
JSONException.class,
() -> new JSONObject(tok, strict));
assertTrue(exception.getMessage().contains(
"Unparsed characters found at end of input text"));
}
@Test
public void strictModeShouldCheckTrailingCharactersAfterConsumedWhitespace() {
JSONParserConfiguration strict =
new JSONParserConfiguration().withStrictMode();
JSONTokener tok = new JSONTokener(" {}xxx");
tok.next();
tok.next();
tok.back();
JSONException exception = assertThrows(
JSONException.class,
() -> new JSONObject(tok, strict));
assertTrue(exception.getMessage().contains(
"Unparsed characters found at end of input text"));
}
@Test
public void strictModeShouldCheckTrailingCharactersAfterNextCleanAndBack() {
JSONParserConfiguration strict =
new JSONParserConfiguration().withStrictMode();
JSONTokener tok = new JSONTokener(" {}xxx");
tok.nextClean();
tok.back();
JSONException exception = assertThrows(
JSONException.class,
() -> new JSONObject(tok, strict));
assertTrue(exception.getMessage().contains(
"Unparsed characters found at end of input text"));
}
}