Fixed [JACKSON-832] (or at least Long case)
diff --git a/release-notes/CREDITS b/release-notes/CREDITS index 55cab1b..67546e2 100644 --- a/release-notes/CREDITS +++ b/release-notes/CREDITS
@@ -894,3 +894,7 @@ * Reported [JACKSON-829] Custom serializers not working for List<String> properties, @JsonSerialize(contentUsing) [1.9.7] + +Jan Jan: + * Reported [JACKSON-832] (partial) Fix numeric range check for Longs (was not working) + [1.9.7]
diff --git a/release-notes/VERSION b/release-notes/VERSION index cc4001a..664bd29 100644 --- a/release-notes/VERSION +++ b/release-notes/VERSION
@@ -19,6 +19,8 @@ * [JACKSON-829] Custom serializers not working for List<String> properties, @JsonSerialize(contentUsing) (reported by James R) + * [JACKSON-832] (partial) Fix numeric range check for Longs (was not working) + (reported by Jan J) ------------------------------------------------------------------------ === History: ===
diff --git a/src/java/org/codehaus/jackson/impl/JsonParserBase.java b/src/java/org/codehaus/jackson/impl/JsonParserBase.java index 18a9201..4103677 100644 --- a/src/java/org/codehaus/jackson/impl/JsonParserBase.java +++ b/src/java/org/codehaus/jackson/impl/JsonParserBase.java
@@ -192,11 +192,17 @@ // Also, we need some numeric constants - final static BigDecimal BD_MIN_LONG = new BigDecimal(Long.MIN_VALUE); - final static BigDecimal BD_MAX_LONG = new BigDecimal(Long.MAX_VALUE); + final static BigInteger BI_MIN_INT = BigInteger.valueOf(Integer.MIN_VALUE); + final static BigInteger BI_MAX_INT = BigInteger.valueOf(Integer.MAX_VALUE); - final static BigDecimal BD_MIN_INT = new BigDecimal(Long.MIN_VALUE); - final static BigDecimal BD_MAX_INT = new BigDecimal(Long.MAX_VALUE); + final static BigInteger BI_MIN_LONG = BigInteger.valueOf(Long.MIN_VALUE); + final static BigInteger BI_MAX_LONG = BigInteger.valueOf(Long.MAX_VALUE); + + final static BigDecimal BD_MIN_LONG = new BigDecimal(BI_MIN_LONG); + final static BigDecimal BD_MAX_LONG = new BigDecimal(BI_MAX_LONG); + + final static BigDecimal BD_MIN_INT = new BigDecimal(BI_MIN_INT); + final static BigDecimal BD_MAX_INT = new BigDecimal(BI_MAX_INT); final static long MIN_INT_L = (long) Integer.MIN_VALUE; final static long MAX_INT_L = (long) Integer.MAX_VALUE; @@ -824,7 +830,10 @@ } _numberInt = result; } else if ((_numTypesValid & NR_BIGINT) != 0) { - // !!! Should check for range... + if (BI_MIN_INT.compareTo(_numberBigInt) > 0 + || BI_MAX_INT.compareTo(_numberBigInt) < 0) { + reportOverflowInt(); + } _numberInt = _numberBigInt.intValue(); } else if ((_numTypesValid & NR_DOUBLE) != 0) { // Need to check boundaries @@ -851,7 +860,10 @@ if ((_numTypesValid & NR_INT) != 0) { _numberLong = (long) _numberInt; } else if ((_numTypesValid & NR_BIGINT) != 0) { - // !!! Should check for range... + if (BI_MIN_LONG.compareTo(_numberBigInt) > 0 + || BI_MAX_LONG.compareTo(_numberBigInt) < 0) { + reportOverflowLong(); + } _numberLong = _numberBigInt.longValue(); } else if ((_numTypesValid & NR_DOUBLE) != 0) { // Need to check boundaries
diff --git a/src/test/org/codehaus/jackson/main/TestNumericValues.java b/src/test/org/codehaus/jackson/main/TestNumericValues.java index 799175c..85a5541 100644 --- a/src/test/org/codehaus/jackson/main/TestNumericValues.java +++ b/src/test/org/codehaus/jackson/main/TestNumericValues.java
@@ -242,6 +242,49 @@ } } + public void testLongOverflow() throws Exception + { + BigInteger below = BigInteger.valueOf(Long.MIN_VALUE); + below = below.subtract(BigInteger.ONE); + BigInteger above = BigInteger.valueOf(Long.MAX_VALUE); + above = above.add(BigInteger.ONE); + + String DOC_BELOW = below.toString() + " "; + String DOC_ABOVE = below.toString() + " "; + for (int input = 0; input < 2; ++input) { + JsonParser jp; + + if (input == 0) { + jp = createParserUsingStream(DOC_BELOW, "UTF-8"); + } else { + jp = createParserUsingReader(DOC_BELOW); + } + jp.nextToken(); + try { + long x = jp.getLongValue(); + fail("Expected an exception for underflow (input "+jp.getText()+"): instead, got long value: "+x); + } catch (JsonParseException e) { + verifyException(e, "out of range of long"); + } + jp.close(); + + if (input == 0) { + jp = createParserUsingStream(DOC_ABOVE, "UTF-8"); + } else { + jp = createParserUsingReader(DOC_ABOVE); + } + jp.nextToken(); + try { + long x = jp.getLongValue(); + fail("Expected an exception for underflow (input "+jp.getText()+"): instead, got long value: "+x); + } catch (JsonParseException e) { + verifyException(e, "out of range of long"); + } + jp.close(); + + } + } + /** * Method that tries to test that number parsing works in cases where * input is split between buffer boundaries.
diff --git a/src/test/org/codehaus/jackson/smile/TestSmileParserLocation.java b/src/test/org/codehaus/jackson/smile/TestSmileParserLocation.java index ca9d29f..299e905 100644 --- a/src/test/org/codehaus/jackson/smile/TestSmileParserLocation.java +++ b/src/test/org/codehaus/jackson/smile/TestSmileParserLocation.java
@@ -22,7 +22,8 @@ assertNotNull(loc); // first: -1 for "not known", for character-based stuff assertEquals(-1, loc.getCharOffset()); - assertEquals(-1, loc.getColumnNr()); + // except that with 1.9.7 and above, we also consider column to be same as offset, for convenience + assertEquals(4, loc.getColumnNr()); assertEquals(-1, loc.getLineNr()); // but first 4 bytes are for header assertEquals(4, loc.getByteOffset());