Skip to content

Commit cba329f

Browse files
Handle value overflow checks for IonParser.getIntValue() - #428 (#481)
1 parent 4974cfd commit cba329f

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

ion/src/main/java/com/fasterxml/jackson/dataformat/ion/IonParser.java

+26-2
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,23 @@ public int getIntValue() throws IOException {
381381
// @since 2.17
382382
private int _getIntValue() throws IOException {
383383
try {
384-
return _reader.intValue();
384+
NumberType numberType = getNumberType();
385+
if (numberType == NumberType.LONG) {
386+
int result = _reader.intValue();
387+
if ((long) result != _reader.longValue()) {
388+
this.reportOverflowInt();
389+
}
390+
return result;
391+
}
392+
if (numberType == NumberType.BIG_INTEGER) {
393+
BigInteger bigInteger = _reader.bigIntegerValue();
394+
if (BI_MIN_INT.compareTo(bigInteger) > 0 || BI_MAX_INT.compareTo(bigInteger) < 0) {
395+
this.reportOverflowInt();
396+
}
397+
return bigInteger.intValue();
398+
} else {
399+
return _reader.intValue();
400+
}
385401
} catch (IonException e) {
386402
return _reportCorruptNumber(e);
387403
}
@@ -396,7 +412,15 @@ public long getLongValue() throws IOException {
396412
// @since 2.17
397413
private long _getLongValue() throws IOException {
398414
try {
399-
return _reader.longValue();
415+
if (this.getNumberType() == NumberType.BIG_INTEGER) {
416+
BigInteger bigInteger = _reader.bigIntegerValue();
417+
if (BI_MIN_INT.compareTo(bigInteger) > 0 || BI_MAX_INT.compareTo(bigInteger) < 0) {
418+
this.reportOverflowLong();
419+
}
420+
return bigInteger.longValue();
421+
} else {
422+
return _reader.longValue();
423+
}
400424
} catch (IonException e) {
401425
return _reportCorruptNumber(e);
402426
}
+2-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
package com.fasterxml.jackson.dataformat.ion.failing;
1+
package com.fasterxml.jackson.dataformat.ion;
22

33
import org.hamcrest.Matchers;
44
import org.junit.Test;
55

66
import com.fasterxml.jackson.core.exc.InputCoercionException;
7-
import com.fasterxml.jackson.dataformat.ion.*;
87

98
import static org.hamcrest.MatcherAssert.assertThat;
109
import static org.junit.Assert.assertEquals;
@@ -13,7 +12,7 @@
1312
import java.math.BigInteger;
1413

1514
// for [dataformats-ion#428]
16-
public class IonNumberOverflow428Test
15+
public class IonNumberOverflowTest
1716
{
1817
private final IonObjectMapper MAPPER = IonObjectMapper
1918
.builderForBinaryWriters()

release-notes/CREDITS-2.x

+5
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,8 @@ Arthur Chan (@arthurscchan)
314314
* Contributed #464: (cbor) Unexpected `ArrayIndexOutOfBoundsException` in `CBORParser`
315315
for corrupt String value
316316
(2.17.0)
317+
318+
Thomas de Lange (@thomasdelange5)
319+
* Contributed fix for #428: (ion) `IonParser.getIntValue()` fails or does not handle
320+
value overflow checks
321+
(2.17.0)

release-notes/VERSION-2.x

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ Active maintainers:
3131
#424: (ion) `IonReader` throws `NullPointerException` for unchecked invalid data
3232
(fix contributed by Arthur C)
3333
#426: (smile) `SmileParser` throws unexpected IOOBE for corrupt content
34-
(fix contributed by Arthur C)-(
34+
(fix contributed by Arthur C)
35+
#428: (ion) `IonParser.getIntValue()` fails or does not handle value overflow checks
36+
(fix contributed by Thomas d-L)
3537
#432: (ion) More methods from `IonReader` could throw an unexpected `AssertionError`
3638
(fix contributed by Arthur C)
3739
#434: (ion) Unexpected `NullPointerException` thrown from `IonParser::getNumberType()`

0 commit comments

Comments
 (0)