From 4c2980e6ff798f3209f00292ead5060c402d9774 Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Mon, 27 Aug 2012 02:34:52 +0000 Subject: [PATCH] Fix a bug in integer parsing. --- .../com/google/gson/stream/JsonReader.java | 4 ++- .../google/gson/stream/JsonReaderTest.java | 34 ++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) 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 9649230c..37fa68dc 100644 --- a/gson/src/main/java/com/google/gson/stream/JsonReader.java +++ b/gson/src/main/java/com/google/gson/stream/JsonReader.java @@ -190,6 +190,7 @@ import java.io.Reader; public class JsonReader implements Closeable { /** The only non-execute prefix this parser permits */ private static final char[] NON_EXECUTE_PREFIX = ")]}'\n".toCharArray(); + private static final long MIN_INCOMPLETE_INTEGER = Long.MIN_VALUE / 10; private static final int PEEKED_NONE = 0; private static final int PEEKED_BEGIN_OBJECT = 1; @@ -647,7 +648,8 @@ public class JsonReader implements Closeable { c = get(++i); while (c >= '0' && c <= '9') { long newInteger = integer * 10 - (c - '0'); - fitsInLong &= newInteger < integer; + fitsInLong = integer > MIN_INCOMPLETE_INTEGER + || (integer == MIN_INCOMPLETE_INTEGER && newInteger < integer); integer = newInteger; c = get(++i); } 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 cddec2b6..81df7565 100644 --- a/gson/src/test/java/com/google/gson/stream/JsonReaderTest.java +++ b/gson/src/test/java/com/google/gson/stream/JsonReaderTest.java @@ -422,11 +422,43 @@ public final class JsonReaderTest extends TestCase { assertEquals(-9223372036854775808L, reader.nextLong()); } + public void testPeekLongMaxValue() throws IOException { + JsonReader reader = new JsonReader(new StringReader("[9223372036854775807]")); + reader.setLenient(true); + reader.beginArray(); + assertEquals(NUMBER, reader.peek()); + assertEquals(9223372036854775807L, reader.nextLong()); + } + + public void testLongLargerThanMaxLongThatWrapsAround() throws IOException { + JsonReader reader = new JsonReader(new StringReader("[22233720368547758070]")); + reader.setLenient(true); + reader.beginArray(); + assertEquals(NUMBER, reader.peek()); + try { + reader.nextLong(); + fail(); + } catch (NumberFormatException expected) { + } + } + + public void testLongLargerThanMinLongThatWrapsAround() throws IOException { + JsonReader reader = new JsonReader(new StringReader("[-22233720368547758070]")); + reader.setLenient(true); + reader.beginArray(); + assertEquals(NUMBER, reader.peek()); + try { + reader.nextLong(); + fail(); + } catch (NumberFormatException expected) { + } + } + /** * This test fails because there's no double for -9223372036854775809, and our * long parsing uses Double.parseDouble() for fractional values. */ - public void disabled_testPeekLargerThanLongMinValue() throws IOException { + public void testPeekLargerThanLongMinValue() throws IOException { JsonReader reader = new JsonReader(new StringReader("[-9223372036854775809]")); reader.setLenient(true); reader.beginArray();