Merge branch 'master' into feat/871-strictMode

This commit is contained in:
rikkarth 2024-03-30 22:03:57 +00:00
commit d92d62afc2
No known key found for this signature in database
GPG Key ID: 11E5F28B0AED6AC7
3 changed files with 95 additions and 38 deletions

View File

@ -297,45 +297,48 @@ public class JSONTokener {
for (;;) { for (;;) {
c = this.next(); c = this.next();
switch (c) { switch (c) {
case 0: case 0:
case '\n': case '\n':
case '\r': case '\r':
throw this.syntaxError("Unterminated string"); throw this.syntaxError("Unterminated string. " +
case '\\': "Character with int code " + (int) c + " is not allowed within a quoted string.");
c = this.next(); case '\\':
switch (c) { c = this.next();
case 'b': switch (c) {
sb.append('\b'); case 'b':
break; sb.append('\b');
case 't': break;
sb.append('\t'); case 't':
break; sb.append('\t');
case 'n': break;
sb.append('\n'); case 'n':
break; sb.append('\n');
case 'f': break;
sb.append('\f'); case 'f':
break; sb.append('\f');
case 'r': break;
sb.append('\r'); case 'r':
break; sb.append('\r');
case 'u': break;
try { case 'u':
sb.append((char) Integer.parseInt(this.next(4), 16)); String next = this.next(4);
} catch (NumberFormatException e) { try {
throw this.syntaxError("Illegal escape.", e); sb.append((char)Integer.parseInt(next, 16));
} } catch (NumberFormatException e) {
break; throw this.syntaxError("Illegal escape. " +
case '"': "\\u must be followed by a 4 digit hexadecimal number. \\" + next + " is not valid.", e);
case '\'':
case '\\':
case '/':
sb.append(c);
break;
default:
throw this.syntaxError("Illegal escape.");
} }
break; break;
case '"':
case '\'':
case '\\':
case '/':
sb.append(c);
break;
default:
throw this.syntaxError("Illegal escape. Escape sequence \\" + c + " is not valid.");
}
break;
default: default:
if (strictMode && c == '\"' && quote != c) { if (strictMode && c == '\"' && quote != c) {
throw this.syntaxError(String.format( throw this.syntaxError(String.format(

View File

@ -2193,6 +2193,60 @@ public class JSONObjectTest {
} }
} }
@Test
public void jsonObjectParseControlCharacterEOFAssertExceptionMessage(){
char c = '\0';
final String source = "{\"key\":\"" + c + "\"}";
try {
JSONObject jo = new JSONObject(source);
fail("JSONException should be thrown");
} catch (JSONException ex) {
assertEquals("Unterminated string. " + "Character with int code 0" +
" is not allowed within a quoted string. at 8 [character 9 line 1]", ex.getMessage());
}
}
@Test
public void jsonObjectParseControlCharacterNewLineAssertExceptionMessage(){
char[] chars = {'\n', '\r'};
for( char c : chars) {
final String source = "{\"key\":\"" + c + "\"}";
try {
JSONObject jo = new JSONObject(source);
fail("JSONException should be thrown");
} catch (JSONException ex) {
assertEquals("Unterminated string. " + "Character with int code " + (int) c +
" is not allowed within a quoted string. at 9 [character 0 line 2]", ex.getMessage());
}
}
}
@Test
public void jsonObjectParseUTF8EncodingAssertExceptionMessage(){
String c = "\\u123x";
final String source = "{\"key\":\"" + c + "\"}";
try {
JSONObject jo = new JSONObject(source);
fail("JSONException should be thrown");
} catch (JSONException ex) {
assertEquals("Illegal escape. \\u must be followed by a 4 digit hexadecimal number. " +
"\\123x is not valid. at 14 [character 15 line 1]", ex.getMessage());
}
}
@Test
public void jsonObjectParseIllegalEscapeAssertExceptionMessage(){
String c = "\\x";
final String source = "{\"key\":\"" + c + "\"}";
try {
JSONObject jo = new JSONObject(source);
fail("JSONException should be thrown");
} catch (JSONException ex) {
assertEquals("Illegal escape. Escape sequence " + c + " is not valid." +
" at 10 [character 11 line 1]", ex.getMessage());
}
}
/** /**
* Explore how JSONObject handles parsing errors. * Explore how JSONObject handles parsing errors.
*/ */

View File

@ -164,7 +164,7 @@ public class JSONParserConfigurationTest {
() -> new JSONArray(testCaseTwo, jsonParserConfiguration)); () -> new JSONArray(testCaseTwo, jsonParserConfiguration));
assertEquals("Expected a ',' or ']' at 10 [character 11 line 1]", jeOne.getMessage()); assertEquals("Expected a ',' or ']' at 10 [character 11 line 1]", jeOne.getMessage());
assertEquals("Unterminated string at 15 [character 16 line 1]", jeTwo.getMessage()); assertEquals("Unterminated string. Character with int code 0 is not allowed within a quoted string. at 15 [character 16 line 1]", jeTwo.getMessage());
} }