diff --git a/gson/src/main/java/com/google/gson/stream/JsonReader.java b/gson/src/main/java/com/google/gson/stream/JsonReader.java index 0c12b8a6..aef278bd 100644 --- a/gson/src/main/java/com/google/gson/stream/JsonReader.java +++ b/gson/src/main/java/com/google/gson/stream/JsonReader.java @@ -863,6 +863,12 @@ public class JsonReader implements Closeable { return result; } + /** + * Returns the next character in the stream that is neither whitespace nor a + * part of a comment. When this returns, the returned character is always at + * {@code buffer[pos-1]}; this means the caller can always push back the + * returned character by decrementing {@code pos}. + */ private int nextNonWhitespace(boolean throwOnEof) throws IOException { /* * This code uses ugly local variables 'p' and 'l' representing the 'pos' @@ -895,8 +901,13 @@ public class JsonReader implements Closeable { case '/': pos = p; - if (p == l && !fillBuffer(1)) { - return c; + if (p == l) { + pos--; // push back '/' so it's still in the buffer when this method returns + boolean charsLoaded = fillBuffer(2); + pos++; // consume the '/' again + if (!charsLoaded) { + return c; + } } checkLenient(); diff --git a/gson/src/test/java/com/google/gson/stream/JsonReaderTest.java b/gson/src/test/java/com/google/gson/stream/JsonReaderTest.java index 4f73c238..102acc4d 100644 --- a/gson/src/test/java/com/google/gson/stream/JsonReaderTest.java +++ b/gson/src/test/java/com/google/gson/stream/JsonReaderTest.java @@ -1092,7 +1092,21 @@ public final class JsonReaderTest extends TestCase { public void testStringEndingInSlash() throws IOException { JsonReader reader = new JsonReader(new StringReader("/")); reader.setLenient(true); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + try { + reader.peek(); + fail(); + } catch (MalformedJsonException expected) { + } + } + + public void testDocumentWithCommentEndingInSlash() throws IOException { + JsonReader reader = new JsonReader(new StringReader("/* foo *//")); + reader.setLenient(true); + try { + reader.peek(); + fail(); + } catch (MalformedJsonException expected) { + } } public void testStringWithLeadingSlash() throws IOException {