Skip to content

Commit 18b9655

Browse files
authoredJan 1, 2024
Fixes #437: catch and re-throw NPE (#438)

File tree

6 files changed

+72
-7
lines changed

6 files changed

+72
-7
lines changed
 

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -593,8 +593,8 @@ public JsonToken nextToken() throws IOException
593593
} catch (IonException e) {
594594
return _reportCorruptContent(e);
595595

596-
} catch (IndexOutOfBoundsException | AssertionError e) {
597-
// [dataformats-binary#420]: IonJava leaks IOOBEs so:
596+
} catch (AssertionError | IndexOutOfBoundsException | NullPointerException e) {
597+
// [dataformats-binary#420]: IonJava leaks IOOBEs, catch
598598
// [dataformats-binary#432]: AssertionError if we're trying to get the text
599599
// with a symbol id less than or equals to 0.
600600
return _reportCorruptContent(e);

‎ion/src/test/java/com/fasterxml/jackson/dataformat/ion/fuzz/Fuzz434_65268_65274_NPETest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public class Fuzz434_65268_65274_NPETest
2424
// Test that used to fail on "getNumberType()" for `JsonToken.VALUE_NULL`
2525
@Test
2626
public void testFuzz65268() throws Exception {
27-
try (InputStream in = getClass().getResourceAsStream("/data/fuzz-65268.ion")) {
27+
final byte[] doc = IonFuzzTestUtil.readResource("/data/fuzz-65268.ion");
28+
try (InputStream in = new ByteArrayInputStream(doc)) {
2829
try (JsonParser p = ION_MAPPER.createParser(in)) {
2930
assertEquals(JsonToken.VALUE_STRING, p.nextToken());
3031
p.getText();
@@ -37,10 +38,9 @@ public void testFuzz65268() throws Exception {
3738

3839
@Test
3940
public void testFuzz65274Malformed() throws Exception {
40-
try (InputStream in = getClass().getResourceAsStream("/data/fuzz-65274.ion")) {
41-
byte[] invalid = new byte[in.available()];
42-
new DataInputStream(in).readFully(invalid);
43-
ION_MAPPER.readTree(new ByteArrayInputStream(invalid));
41+
final byte[] doc = IonFuzzTestUtil.readResource("/data/fuzz-65274.ion");
42+
try {
43+
ION_MAPPER.readTree(new ByteArrayInputStream(doc));
4444
fail("Should not pass (invalid content)");
4545
} catch (StreamReadException e) {
4646
assertThat(e.getMessage(), Matchers.containsString("Corrupt content to decode"));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fasterxml.jackson.dataformat.ion.fuzz;
2+
3+
import java.io.*;
4+
5+
import org.hamcrest.Matchers;
6+
import org.junit.Test;
7+
8+
import com.fasterxml.jackson.core.JsonParser;
9+
import com.fasterxml.jackson.core.exc.StreamReadException;
10+
import com.fasterxml.jackson.databind.ObjectMapper;
11+
import com.fasterxml.jackson.dataformat.ion.*;
12+
13+
import static org.hamcrest.MatcherAssert.assertThat;
14+
import static org.junit.Assert.fail;
15+
16+
// [dataformats-binary#437]
17+
public class Fuzz437_65452_NPETest
18+
{
19+
private final ObjectMapper ION_MAPPER = new IonObjectMapper();
20+
21+
@Test
22+
public void testFuzz65452NPE() throws Exception {
23+
final byte[] doc = IonFuzzTestUtil.readResource("/data/fuzz-65452.ion");
24+
try (InputStream in = new ByteArrayInputStream(doc)) {
25+
try (JsonParser p = ION_MAPPER.createParser(in)) {
26+
p.nextToken();
27+
}
28+
fail("Should not pass (invalid content)");
29+
} catch (StreamReadException e) {
30+
assertThat(e.getMessage(), Matchers.containsString("Corrupt content to decode"));
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fasterxml.jackson.dataformat.ion.fuzz;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
7+
public class IonFuzzTestUtil
8+
{
9+
public static byte[] readResource(String ref)
10+
{
11+
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
12+
final byte[] buf = new byte[4000];
13+
14+
InputStream in = IonFuzzTestUtil.class.getResourceAsStream(ref);
15+
if (in != null) {
16+
try {
17+
int len;
18+
while ((len = in.read(buf)) > 0) {
19+
bytes.write(buf, 0, len);
20+
}
21+
in.close();
22+
} catch (IOException e) {
23+
throw new RuntimeException("Failed to read resource '"+ref+"': "+e);
24+
}
25+
}
26+
if (bytes.size() == 0) {
27+
throw new IllegalArgumentException("Failed to read resource '"+ref+"': empty resource?");
28+
}
29+
return bytes.toByteArray();
30+
}
31+
}
26 Bytes
Binary file not shown.

‎release-notes/VERSION-2.x

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Active maintainers:
3030
(fix contributed by Arthur C)
3131
#434 (ion) Unexpected `NullPointerException` thrown from `IonParser::getNumberType()`
3232
(fix contributed by Arthur C)
33+
#437 (ion) `IonReader.next()` throws NPEs for some invalid content
3334
- (ion) Update `com.amazon.ion:ion-java` to 1.11.0 (from 1.10.5)
3435

3536
2.16.1 (24-Dec-2023)

0 commit comments

Comments
 (0)
Please sign in to comment.