Skip to content

Commit d92d62a

Browse files
committed
Merge branch 'master' into feat/871-strictMode
2 parents 46534b5 + 87406e4 commit d92d62a

File tree

3 files changed

+95
-38
lines changed

3 files changed

+95
-38
lines changed

src/main/java/org/json/JSONTokener.java

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -297,45 +297,48 @@ public String nextString(char quote, boolean strictMode) throws JSONException {
297297
for (;;) {
298298
c = this.next();
299299
switch (c) {
300-
case 0:
301-
case '\n':
302-
case '\r':
303-
throw this.syntaxError("Unterminated string");
304-
case '\\':
305-
c = this.next();
306-
switch (c) {
307-
case 'b':
308-
sb.append('\b');
309-
break;
310-
case 't':
311-
sb.append('\t');
312-
break;
313-
case 'n':
314-
sb.append('\n');
315-
break;
316-
case 'f':
317-
sb.append('\f');
318-
break;
319-
case 'r':
320-
sb.append('\r');
321-
break;
322-
case 'u':
323-
try {
324-
sb.append((char) Integer.parseInt(this.next(4), 16));
325-
} catch (NumberFormatException e) {
326-
throw this.syntaxError("Illegal escape.", e);
327-
}
328-
break;
329-
case '"':
330-
case '\'':
331-
case '\\':
332-
case '/':
333-
sb.append(c);
334-
break;
335-
default:
336-
throw this.syntaxError("Illegal escape.");
300+
case 0:
301+
case '\n':
302+
case '\r':
303+
throw this.syntaxError("Unterminated string. " +
304+
"Character with int code " + (int) c + " is not allowed within a quoted string.");
305+
case '\\':
306+
c = this.next();
307+
switch (c) {
308+
case 'b':
309+
sb.append('\b');
310+
break;
311+
case 't':
312+
sb.append('\t');
313+
break;
314+
case 'n':
315+
sb.append('\n');
316+
break;
317+
case 'f':
318+
sb.append('\f');
319+
break;
320+
case 'r':
321+
sb.append('\r');
322+
break;
323+
case 'u':
324+
String next = this.next(4);
325+
try {
326+
sb.append((char)Integer.parseInt(next, 16));
327+
} catch (NumberFormatException e) {
328+
throw this.syntaxError("Illegal escape. " +
329+
"\\u must be followed by a 4 digit hexadecimal number. \\" + next + " is not valid.", e);
337330
}
338331
break;
332+
case '"':
333+
case '\'':
334+
case '\\':
335+
case '/':
336+
sb.append(c);
337+
break;
338+
default:
339+
throw this.syntaxError("Illegal escape. Escape sequence \\" + c + " is not valid.");
340+
}
341+
break;
339342
default:
340343
if (strictMode && c == '\"' && quote != c) {
341344
throw this.syntaxError(String.format(

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,6 +2193,60 @@ public void jsonObjectParseControlCharacters(){
21932193
}
21942194
}
21952195

2196+
@Test
2197+
public void jsonObjectParseControlCharacterEOFAssertExceptionMessage(){
2198+
char c = '\0';
2199+
final String source = "{\"key\":\"" + c + "\"}";
2200+
try {
2201+
JSONObject jo = new JSONObject(source);
2202+
fail("JSONException should be thrown");
2203+
} catch (JSONException ex) {
2204+
assertEquals("Unterminated string. " + "Character with int code 0" +
2205+
" is not allowed within a quoted string. at 8 [character 9 line 1]", ex.getMessage());
2206+
}
2207+
}
2208+
2209+
@Test
2210+
public void jsonObjectParseControlCharacterNewLineAssertExceptionMessage(){
2211+
char[] chars = {'\n', '\r'};
2212+
for( char c : chars) {
2213+
final String source = "{\"key\":\"" + c + "\"}";
2214+
try {
2215+
JSONObject jo = new JSONObject(source);
2216+
fail("JSONException should be thrown");
2217+
} catch (JSONException ex) {
2218+
assertEquals("Unterminated string. " + "Character with int code " + (int) c +
2219+
" is not allowed within a quoted string. at 9 [character 0 line 2]", ex.getMessage());
2220+
}
2221+
}
2222+
}
2223+
2224+
@Test
2225+
public void jsonObjectParseUTF8EncodingAssertExceptionMessage(){
2226+
String c = "\\u123x";
2227+
final String source = "{\"key\":\"" + c + "\"}";
2228+
try {
2229+
JSONObject jo = new JSONObject(source);
2230+
fail("JSONException should be thrown");
2231+
} catch (JSONException ex) {
2232+
assertEquals("Illegal escape. \\u must be followed by a 4 digit hexadecimal number. " +
2233+
"\\123x is not valid. at 14 [character 15 line 1]", ex.getMessage());
2234+
}
2235+
}
2236+
2237+
@Test
2238+
public void jsonObjectParseIllegalEscapeAssertExceptionMessage(){
2239+
String c = "\\x";
2240+
final String source = "{\"key\":\"" + c + "\"}";
2241+
try {
2242+
JSONObject jo = new JSONObject(source);
2243+
fail("JSONException should be thrown");
2244+
} catch (JSONException ex) {
2245+
assertEquals("Illegal escape. Escape sequence " + c + " is not valid." +
2246+
" at 10 [character 11 line 1]", ex.getMessage());
2247+
}
2248+
}
2249+
21962250
/**
21972251
* Explore how JSONObject handles parsing errors.
21982252
*/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public void givenUnbalancedQuotes_testStrictModeFalse_shouldThrowJsonException()
164164
() -> new JSONArray(testCaseTwo, jsonParserConfiguration));
165165

166166
assertEquals("Expected a ',' or ']' at 10 [character 11 line 1]", jeOne.getMessage());
167-
assertEquals("Unterminated string at 15 [character 16 line 1]", jeTwo.getMessage());
167+
assertEquals("Unterminated string. Character with int code 0 is not allowed within a quoted string. at 15 [character 16 line 1]", jeTwo.getMessage());
168168
}
169169

170170

0 commit comments

Comments
 (0)