Skip to content

Commit

Permalink
#5 Add test case for unescaped control characters
Browse files Browse the repository at this point in the history
* Feat #5: add new lines to make JsonReader able to detect unescaped control characters (U+0000 through U+001F) and throw exceptions.

* Feat #5: add new lines to make JsonReader able to detect unescaped control characters (U+0000 through U+001F) and throw exceptions.

* Test #11: Added two tests for testing implementation of control character handling in strict mode and moved the implementation to nextQuotedValue

* Test #11: Added two tests for testing implementation of control character handling in strict mode and moved the implementation to nextQuotedValue

---------

Co-authored-by: LMC117 <2295699210@qq.com>
Co-authored-by: Marten Voorberg <martenvoorberg@gmail.com>
  • Loading branch information
3 people committed Feb 27, 2023
1 parent f2abd89 commit d506d00
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions gson/src/main/java/com/google/gson/stream/JsonReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,7 @@ private String nextQuotedValue(char quote) throws IOException {
while (p < l) {
int c = buffer[p++];

// In strict mode, throw an exception when meeting unescaped control characters (U+0000 through U+001F)
if (strictness == Strictness.STRICT && c < 0x20) {
throw syntaxError("Unescaped control characters (\\u0000-\\u001F) are not allowed in strict mode.");
} else if (c == quote) {
Expand Down
14 changes: 14 additions & 0 deletions gson/src/test/java/com/google/gson/stream/JsonReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static com.google.gson.stream.JsonToken.NUMBER;
import static com.google.gson.stream.JsonToken.STRING;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.EOFException;
Expand Down Expand Up @@ -477,6 +478,19 @@ public void testUnescapingInvalidCharacters() throws IOException {
}
}

@Test
public void testUnescapedControlCharactersInStrictMode() throws IOException {
String json = "[\"\u0014\"]";
JsonReader reader = new JsonReader(reader(json));
reader.setStrict(true);
reader.beginArray();
try {
reader.nextString();
} catch (IOException expected) {
assertTrue(expected.getMessage().contains("Unescaped control characters"));
}
}

@Test
public void testUnescapingTruncatedCharacters() throws IOException {
String json = "[\"\\u000";
Expand Down

0 comments on commit d506d00

Please sign in to comment.