Skip to content

Commit a0c01db

Browse files
authored
Fixes for #424: Add null checking and wrap NPE (#425)
1 parent db12a65 commit a0c01db

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

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

+18-1
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,10 @@ public String getText() throws IOException
278278
// trying to get the text for a symbol id that cannot be resolved.
279279
// stringValue() has an assert statement which could throw an
280280
throw _constructError(e.getMessage(), e);
281-
} catch (AssertionError e) {
281+
} catch (AssertionError | NullPointerException e) {
282282
// AssertionError if we're trying to get the text with a symbol
283283
// id less than or equals to 0.
284+
// NullPointerException may also be thrown on invalid data
284285
String msg = e.getMessage();
285286
if (msg == null) {
286287
msg = "UNKNOWN ROOT CAUSE";
@@ -331,34 +332,50 @@ public int getTextOffset() throws IOException {
331332

332333
@Override
333334
public BigInteger getBigIntegerValue() throws IOException {
335+
_verifyIsNumberToken();
334336
return _reader.bigIntegerValue();
335337
}
336338

337339
@Override
338340
public BigDecimal getDecimalValue() throws IOException {
341+
_verifyIsNumberToken();
339342
return _reader.bigDecimalValue();
340343
}
341344

342345
@Override
343346
public double getDoubleValue() throws IOException {
347+
_verifyIsNumberToken();
344348
return _reader.doubleValue();
345349
}
346350

347351
@Override
348352
public float getFloatValue() throws IOException {
353+
_verifyIsNumberToken();
349354
return (float) _reader.doubleValue();
350355
}
351356

352357
@Override
353358
public int getIntValue() throws IOException {
359+
_verifyIsNumberToken();
354360
return _reader.intValue();
355361
}
356362

357363
@Override
358364
public long getLongValue() throws IOException {
365+
_verifyIsNumberToken();
359366
return _reader.longValue();
360367
}
361368

369+
// @since 2.17
370+
private void _verifyIsNumberToken() throws IOException
371+
{
372+
if (_currToken != JsonToken.VALUE_NUMBER_INT && _currToken != JsonToken.VALUE_NUMBER_FLOAT) {
373+
// Same as `ParserBase._parseNumericValue()` exception:
374+
_reportError("Current token (%s) not numeric, can not use numeric value accessors",
375+
_currToken);
376+
}
377+
}
378+
362379
@Override
363380
public NumberType getNumberType() throws IOException
364381
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.fasterxml.jackson.dataformat.ion.fuzz;
2+
3+
import java.io.ByteArrayInputStream;
4+
5+
import org.hamcrest.Matchers;
6+
import org.junit.Test;
7+
8+
import com.fasterxml.jackson.core.exc.StreamReadException;
9+
import com.fasterxml.jackson.dataformat.ion.*;
10+
11+
import static org.hamcrest.MatcherAssert.assertThat;
12+
import static org.junit.Assert.fail;
13+
14+
// [dataformats-binary#424]
15+
public class Fuzz424_65065_65126NPETest
16+
{
17+
@Test
18+
public void testFuzz65065() throws Exception {
19+
IonFactory f = IonFactory
20+
.builderForBinaryWriters()
21+
.build();
22+
23+
IonObjectMapper mapper = IonObjectMapper.builder(f).build();
24+
25+
try {
26+
byte[] bytes = {(byte) -32, (byte) 1, (byte) 0, (byte) -22, (byte) 123, (byte) -112};
27+
mapper.readTree(f.createParser(new ByteArrayInputStream(bytes)));
28+
fail("Should not pass (invalid content)");
29+
} catch (StreamReadException e) {
30+
assertThat(e.getMessage(), Matchers.containsString("Internal `IonReader` error"));
31+
}
32+
}
33+
34+
@Test
35+
public void testFuzz65126() throws Exception {
36+
IonFactory f = IonFactory
37+
.builderForBinaryWriters()
38+
.build();
39+
40+
try {
41+
byte[] bytes = {(byte) 1, (byte) 0};
42+
f.createParser(bytes).getDecimalValue();
43+
fail("Should not pass (invalid content)");
44+
} catch (StreamReadException e) {
45+
assertThat(e.getMessage(), Matchers.containsString("Current token (null) not numeric"));
46+
}
47+
}
48+
}

release-notes/CREDITS-2.x

+3
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,6 @@ Arthur Chan (@arthurscchan)
287287
(2.17.0)
288288
* Contributed #420: (ion) `IndexOutOfBoundsException` thrown by `IonReader` implementations
289289
(2.17.0)
290+
* Contributed #424: (ion) `IonReader` throws `NullPointerException` for unchecked
291+
invalid data
292+
(2.17.0)

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Active maintainers:
2222
#420: (ion) `IndexOutOfBoundsException` thrown by `IonReader` implementations
2323
are not handled
2424
(contributed by Arthur C)
25+
#424: (ion) `IonReader` throws `NullPointerException` for unchecked invalid data
26+
(contributed by Arthur C)
2527
-(ion) Update `com.amazon.ion:ion-java` to 1.11.0 (from 1.10.5)
2628

2729
2.16.0 (15-Nov-2023)

0 commit comments

Comments
 (0)