google / google/gson

JsonReader.peek advances when exception is thrown

Open
#1,735 0 comments 1 reaction 0 assignees View on GitHub
bug
Dominant language
Java
Stars
24.2k
Forks
4.5k
Avg merge
6d 4h
Merged PRs (30d)
12

Description

When `JsonReader.peek` encounters malformed JSON and throws an exception, it has already modified the state of the reader and advanced one char. This is likely not intended and appears to be the reason why the test `JsonReaderTest.disabled_testNumberWithOctalPrefix` is currently disabled.

Example:
```java
public void testThrowingPeek() throws IOException {
JsonReader reader = new JsonReader(new StringReader("[a?$,1]"));
reader.beginArray();
for (int i = 0; i < 4; i++) {
try {
reader.peek();
} catch (MalformedJsonException expected) {
}
}
reader.nextInt();
reader.endArray();
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.