Fix a bug in integer parsing.

This commit is contained in:
Jesse Wilson 2012-08-27 02:34:52 +00:00
parent c7cb503cdb
commit 4c2980e6ff
2 changed files with 36 additions and 2 deletions

View File

@ -190,6 +190,7 @@ import java.io.Reader;
public class JsonReader implements Closeable { public class JsonReader implements Closeable {
/** The only non-execute prefix this parser permits */ /** The only non-execute prefix this parser permits */
private static final char[] NON_EXECUTE_PREFIX = ")]}'\n".toCharArray(); 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_NONE = 0;
private static final int PEEKED_BEGIN_OBJECT = 1; private static final int PEEKED_BEGIN_OBJECT = 1;
@ -647,7 +648,8 @@ public class JsonReader implements Closeable {
c = get(++i); c = get(++i);
while (c >= '0' && c <= '9') { while (c >= '0' && c <= '9') {
long newInteger = integer * 10 - (c - '0'); long newInteger = integer * 10 - (c - '0');
fitsInLong &= newInteger < integer; fitsInLong = integer > MIN_INCOMPLETE_INTEGER
|| (integer == MIN_INCOMPLETE_INTEGER && newInteger < integer);
integer = newInteger; integer = newInteger;
c = get(++i); c = get(++i);
} }

View File

@ -422,11 +422,43 @@ public final class JsonReaderTest extends TestCase {
assertEquals(-9223372036854775808L, reader.nextLong()); 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 * This test fails because there's no double for -9223372036854775809, and our
* long parsing uses Double.parseDouble() for fractional values. * 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]")); JsonReader reader = new JsonReader(new StringReader("[-9223372036854775809]"));
reader.setLenient(true); reader.setLenient(true);
reader.beginArray(); reader.beginArray();