Apply corner-case fixes under instruction

This commit is contained in:
XIAYM-gh
2026-09-04 08:09:14 +08:00
parent bcbbc89568
commit 359b43d8df
5 changed files with 100 additions and 20 deletions
+4 -4
View File
@@ -103,10 +103,10 @@ public class JSONArray implements Iterable<Object> {
* *
* @param x A JSONTokener instance from which the JSONArray is constructed. * @param x A JSONTokener instance from which the JSONArray is constructed.
* @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser. * @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
* @param eofRequired A boolean that determines whether this array is the root. * @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. * @throws JSONException If a syntax error occurs during the construction of the JSONArray.
*/ */
JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean eofRequired) throws JSONException { JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) throws JSONException {
this(); this();
if (x.nextClean() != '[') { if (x.nextClean() != '[') {
@@ -130,10 +130,10 @@ public class JSONArray implements Iterable<Object> {
x.back(); x.back();
this.myArrayList.add(x.nextValue()); this.myArrayList.add(x.nextValue());
} }
if (checkForSyntaxError(x, jsonParserConfiguration, eofRequired)) return; if (checkForSyntaxError(x, jsonParserConfiguration, isInitial)) return;
} }
} else { } else {
if (eofRequired && jsonParserConfiguration.isStrictMode() && x.nextClean() != 0) { if (isInitial && jsonParserConfiguration.isStrictMode() && x.nextClean() != 0) {
throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text"); throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text");
} }
} }
+3 -3
View File
@@ -225,20 +225,20 @@ public class JSONObject {
* A JSONTokener object containing the source string. * A JSONTokener object containing the source string.
* @param jsonParserConfiguration * @param jsonParserConfiguration
* Variable to pass parser custom configuration for json parsing. * Variable to pass parser custom configuration for json parsing.
* @param eofRequired * @param isInitial
* A boolean that determines whether this object is the root. * A boolean that determines whether this object is the root.
* @throws JSONException * @throws JSONException
* If there is a syntax error in the source string or a * If there is a syntax error in the source string or a
* duplicated key. * duplicated key.
*/ */
JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean eofRequired) throws JSONException { JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) throws JSONException {
this(); this();
if (x.nextClean() != '{') { if (x.nextClean() != '{') {
throw x.syntaxError("A JSONObject text must begin with '{'"); throw x.syntaxError("A JSONObject text must begin with '{'");
} }
for (;;) { for (;;) {
if (parseJSONObject(x, jsonParserConfiguration, eofRequired)) { if (parseJSONObject(x, jsonParserConfiguration, isInitial)) {
return; return;
} }
} }
+17 -4
View File
@@ -31,6 +31,8 @@ public class JSONTokener {
private boolean usePrevious; private boolean usePrevious;
/** the number of characters read in the previous line. */ /** the number of characters read in the previous line. */
private long characterPreviousLine; private long characterPreviousLine;
/** number of non-whitespace characters read from the source. */
private long contentCharCount;
// access to this object is required for strict mode checking // access to this object is required for strict mode checking
private JSONParserConfiguration jsonParserConfiguration; private JSONParserConfiguration jsonParserConfiguration;
@@ -60,6 +62,7 @@ public class JSONTokener {
this.usePrevious = false; this.usePrevious = false;
this.previous = 0; this.previous = 0;
this.index = 0; this.index = 0;
this.contentCharCount = 0;
this.character = 1; this.character = 1;
this.characterPreviousLine = 0; this.characterPreviousLine = 0;
this.line = 1; this.line = 1;
@@ -121,12 +124,14 @@ public class JSONTokener {
} }
/** /**
* Returns whether the tokener is currently positioned at the beginning, * Returns whether the tokener is positioned at the beginning,
* i.e. only whitespace characters (or no characters at all) have been read so far.
* Consuming and backing up over the first characters does not change the result.
* *
* @return true if the current input position is the beginning * @return true if no non-whitespace character has been read
*/ */
public boolean isAtStart() { protected boolean isAtStart() {
return this.index == 0; return this.contentCharCount == 0;
} }
/** /**
@@ -140,6 +145,9 @@ public class JSONTokener {
if (this.usePrevious || this.index <= 0) { if (this.usePrevious || this.index <= 0) {
throw new JSONException("Stepping back two steps is not supported"); throw new JSONException("Stepping back two steps is not supported");
} }
if (this.previous > ' ') {
this.contentCharCount--;
}
this.decrementIndexes(); this.decrementIndexes();
this.usePrevious = true; this.usePrevious = true;
this.eof = false; this.eof = false;
@@ -240,6 +248,9 @@ public class JSONTokener {
return 0; return 0;
} }
this.incrementIndexes(c); this.incrementIndexes(c);
if (c > ' ') {
this.contentCharCount++;
}
this.previous = (char) c; this.previous = (char) c;
return this.previous; return this.previous;
} }
@@ -558,6 +569,7 @@ public class JSONTokener {
long startIndex = this.index; long startIndex = this.index;
long startCharacter = this.character; long startCharacter = this.character;
long startLine = this.line; long startLine = this.line;
long startContentCharCount = this.contentCharCount;
this.reader.mark(1000000); this.reader.mark(1000000);
do { do {
c = this.next(); c = this.next();
@@ -569,6 +581,7 @@ public class JSONTokener {
this.index = startIndex; this.index = startIndex;
this.character = startCharacter; this.character = startCharacter;
this.line = startLine; this.line = startLine;
this.contentCharCount = startContentCharCount;
return 0; return 0;
} }
} while (c != to); } while (c != to);
@@ -1573,7 +1573,6 @@ public class JSONArrayTest {
new JSONParserConfiguration().withStrictMode(); new JSONParserConfiguration().withStrictMode();
JSONTokener tok = new JSONTokener("[]xxx"); JSONTokener tok = new JSONTokener("[]xxx");
tok.next(); tok.next();
tok.back(); tok.back();
@@ -1584,4 +1583,39 @@ public class JSONArrayTest {
assertTrue(exception.getMessage().contains( assertTrue(exception.getMessage().contains(
"Unparsed characters found at end of input text")); "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"));
}
} }
@@ -4343,7 +4343,6 @@ public class JSONObjectTest {
new JSONParserConfiguration().withStrictMode(); new JSONParserConfiguration().withStrictMode();
JSONTokener tok = new JSONTokener("{}xxx"); JSONTokener tok = new JSONTokener("{}xxx");
tok.next(); tok.next();
tok.back(); tok.back();
@@ -4355,4 +4354,38 @@ public class JSONObjectTest {
"Unparsed characters found at end of input text")); "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"));
}
} }