Skip to content

Commit 056cb85

Browse files
authored
Fix #485: add range checks for int and long conversions (#488)
1 parent 76648d0 commit 056cb85

File tree

4 files changed

+41
-21
lines changed

4 files changed

+41
-21
lines changed

csv/src/main/java/com/fasterxml/jackson/dataformat/csv/impl/CsvDecoder.java

+25-13
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,14 @@ public class CsvDecoder
199199
final protected static int NR_DOUBLE = 0x008;
200200
final protected static int NR_BIGDECIMAL = 0x0010;
201201

202-
// Also, we need some numeric constants
202+
// Also, we need some numeric constants (copied from ParserBase)
203203

204+
final static BigInteger BI_MIN_INT = BigInteger.valueOf(Integer.MIN_VALUE);
205+
final static BigInteger BI_MAX_INT = BigInteger.valueOf(Integer.MAX_VALUE);
206+
207+
final static BigInteger BI_MIN_LONG = BigInteger.valueOf(Long.MIN_VALUE);
208+
final static BigInteger BI_MAX_LONG = BigInteger.valueOf(Long.MAX_VALUE);
209+
204210
final static BigDecimal BD_MIN_LONG = new BigDecimal(Long.MIN_VALUE);
205211
final static BigDecimal BD_MAX_LONG = new BigDecimal(Long.MAX_VALUE);
206212

@@ -1372,8 +1378,12 @@ protected void convertNumberToInt() throws IOException
13721378
}
13731379
_numberInt = result;
13741380
} else if ((_numTypesValid & NR_BIGINT) != 0) {
1375-
// !!! Should check for range...
1376-
_numberInt = _getBigInteger().intValue();
1381+
final BigInteger bigInteger = _getBigInteger();
1382+
if (BI_MIN_INT.compareTo(bigInteger) > 0
1383+
|| BI_MAX_INT.compareTo(bigInteger) < 0) {
1384+
reportOverflowInt();
1385+
}
1386+
_numberInt = bigInteger.intValue();
13771387
} else if ((_numTypesValid & NR_DOUBLE) != 0) {
13781388
// Need to check boundaries
13791389
if (_numberDouble < MIN_INT_D || _numberDouble > MAX_INT_D) {
@@ -1399,8 +1409,12 @@ protected void convertNumberToLong() throws IOException
13991409
if ((_numTypesValid & NR_INT) != 0) {
14001410
_numberLong = _numberInt;
14011411
} else if ((_numTypesValid & NR_BIGINT) != 0) {
1402-
// !!! Should check for range...
1403-
_numberLong = _getBigInteger().longValue();
1412+
final BigInteger bigInteger = _getBigInteger();
1413+
if (BI_MIN_LONG.compareTo(bigInteger) > 0
1414+
|| BI_MAX_LONG.compareTo(bigInteger) < 0) {
1415+
reportOverflowLong();
1416+
}
1417+
_numberLong = bigInteger.longValue();
14041418
} else if ((_numTypesValid & NR_DOUBLE) != 0) {
14051419
// Need to check boundaries
14061420
if (_numberDouble < MIN_LONG_D || _numberDouble > MAX_LONG_D) {
@@ -1444,11 +1458,10 @@ protected void convertNumberToBigInteger()
14441458
protected void convertNumberToDouble()
14451459
throws IOException
14461460
{
1447-
/* 05-Aug-2008, tatus: Important note: this MUST start with
1448-
* more accurate representations, since we don't know which
1449-
* value is the original one (others get generated when
1450-
* requested)
1451-
*/
1461+
// 05-Aug-2008, tatus: Important note: this MUST start with
1462+
// more accurate representations, since we don't know which
1463+
// value is the original one (others get generated when
1464+
// requested)
14521465

14531466
if ((_numTypesValid & NR_BIGDECIMAL) != 0) {
14541467
_numberDouble = _getBigDecimal().doubleValue();
@@ -1468,9 +1481,8 @@ protected void convertNumberToDouble()
14681481
protected void convertNumberToBigDecimal() throws IOException
14691482
{
14701483
if ((_numTypesValid & NR_DOUBLE) != 0) {
1471-
/* Let's actually parse from String representation, to avoid
1472-
* rounding errors that non-decimal floating operations could incur
1473-
*/
1484+
// Let's actually parse from String representation, to avoid
1485+
// rounding errors that non-decimal floating operations could incur
14741486
final String text = getText();
14751487
_ioContext.streamReadConstraints().validateFPLength(text.length());
14761488
_numberBigDecimal = NumberInput.parseBigDecimal(

csv/src/test/java/com/fasterxml/jackson/dataformat/csv/failing/IntOverflow485Test.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package com.fasterxml.jackson.dataformat.csv.failing;
22

3-
import java.math.BigInteger;
4-
53
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
6-
import com.fasterxml.jackson.core.exc.StreamReadException;
4+
5+
import com.fasterxml.jackson.databind.DatabindException;
76
import com.fasterxml.jackson.databind.ObjectReader;
7+
88
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
9-
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
109
import com.fasterxml.jackson.dataformat.csv.ModuleTestBase;
1110

1211
public class IntOverflow485Test extends ModuleTestBase
@@ -36,8 +35,9 @@ public void testIntOverflow() throws Exception
3635
try {
3736
Numbers485 result = READER.readValue(csv485("111111111111111111111111111111111111111111", "0"));
3837
fail("Should not pass; got: "+result.intValue);
39-
} catch (StreamReadException e) {
40-
verifyException(e, "CHANGE THIS");
38+
} catch (DatabindException e) { // in 2.x gets wrapped
39+
verifyException(e, "Numeric value");
40+
verifyException(e, "out of range of int");
4141
}
4242
}
4343

@@ -47,8 +47,9 @@ public void testLongOverflow() throws Exception
4747
Numbers485 result = READER.readValue(csv485("0",
4848
"2222222222222222222222222222222222222222"));
4949
fail("Should not pass; got: "+result.longValue);
50-
} catch (StreamReadException e) {
51-
verifyException(e, "CHANGE THIS");
50+
} catch (DatabindException e) { // in 2.x gets wrapped
51+
verifyException(e, "Numeric value");
52+
verifyException(e, "out of range of long");
5253
}
5354
}
5455

release-notes/CREDITS-2.x

+5
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,8 @@ Heiko Boettger (@HeikoBoettger)
277277
* Contributed #482: (yaml) Allow passing `ParserImpl` by a subclass or overwrite the events
278278
(2.18.0)
279279

280+
Burdyug Pavel (@Pavel38l)
281+
282+
* Reported #485: (csv) CSVDecoder: No Long and Int out of range exceptions
283+
(2.18.0)
284+

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Active Maintainers:
2323
(contributed by David P)
2424
#482: (yaml) Allow passing `ParserImpl` by a subclass or overwrite the events
2525
(contributed by Heiko B)
26+
#485: (csv) CSVDecoder: No Long and Int out of range exceptions
27+
(reported by Burdyug P)
2628

2729
2.17.2 (05-Jul-2024)
2830

0 commit comments

Comments
 (0)