Skip to content

Commit 80ae368

Browse files
authored
Fix for 432: Wrap additional AssertionError from IonReader (#433)
1 parent 8437178 commit 80ae368

File tree

8 files changed

+63
-18
lines changed

8 files changed

+63
-18
lines changed

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

+22-15
Original file line numberDiff line numberDiff line change
@@ -273,20 +273,16 @@ public String getText() throws IOException
273273
case VALUE_STRING:
274274
try {
275275
return _reader.stringValue();
276-
} catch (UnknownSymbolException e) {
276+
} catch (UnknownSymbolException
277277
// stringValue() will throw an UnknownSymbolException if we're
278278
// trying to get the text for a symbol id that cannot be resolved.
279279
// stringValue() has an assert statement which could throw an
280-
throw _constructError(e.getMessage(), e);
281-
} catch (AssertionError | NullPointerException e) {
280+
| AssertionError | NullPointerException e
282281
// AssertionError if we're trying to get the text with a symbol
283282
// id less than or equals to 0.
284283
// NullPointerException may also be thrown on invalid data
285-
String msg = e.getMessage();
286-
if (msg == null) {
287-
msg = "UNKNOWN ROOT CAUSE";
288-
}
289-
throw _constructError("Internal `IonReader` error: "+msg, e);
284+
) {
285+
return _reportCorruptContent(e);
290286
}
291287
case VALUE_NUMBER_INT:
292288
case VALUE_NUMBER_FLOAT:
@@ -576,8 +572,11 @@ public JsonToken nextToken() throws IOException
576572
type = _reader.next();
577573
} catch (IonException e) {
578574
return _reportCorruptContent(e);
575+
576+
} catch (IndexOutOfBoundsException | AssertionError e) {
579577
// [dataformats-binary#420]: IonJava leaks IOOBEs so:
580-
} catch (IndexOutOfBoundsException e) {
578+
// [dataformats-binary#432]: AssertionError if we're trying to get the text
579+
// with a symbol id less than or equals to 0.
581580
return _reportCorruptContent(e);
582581
}
583582
if (type == null) {
@@ -717,17 +716,25 @@ protected void _handleEOF() throws JsonParseException
717716
}
718717
}
719718

720-
private <T> T _reportCorruptContent(Exception e) throws IOException
719+
private <T> T _reportCorruptContent(Throwable e) throws IOException
721720
{
722-
final String msg = String.format("Corrupt content to decode; underlying failure: (%s) %s",
723-
e.getClass().getName(), e.getMessage());
721+
String origMsg = e.getMessage();
722+
if (origMsg == null) {
723+
origMsg = "[no exception message]";
724+
}
725+
final String msg = String.format("Corrupt content to decode; underlying `IonReader` problem: (%s) %s",
726+
e.getClass().getName(), origMsg);
724727
throw _constructError(msg, e);
725728
}
726729

727-
private <T> T _reportCorruptNumber(Exception e) throws IOException
730+
private <T> T _reportCorruptNumber(Throwable e) throws IOException
728731
{
729-
final String msg = String.format("Corrupt Number value to decode; underlying failure: (%s) %s",
730-
e.getClass().getName(), e.getMessage());
732+
String origMsg = e.getMessage();
733+
if (origMsg == null) {
734+
origMsg = "[no exception message]";
735+
}
736+
final String msg = String.format("Corrupt Number value to decode; underlying `IonReader` problem: (%s) %s",
737+
e.getClass().getName(), origMsg);
731738
throw _constructError(msg, e);
732739
}
733740

ion/src/test/java/com/fasterxml/jackson/dataformat/ion/fuzz/Fuzz417_64721InvalidIonTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void testFuzz64721AssertionException() throws Exception {
2727
mapper.readValue("$0/", EnumFuzz.class);
2828
fail("Should not pass (invalid content)");
2929
} catch (StreamReadException e) {
30-
assertThat(e.getMessage(), Matchers.containsString("Internal `IonReader` error"));
30+
assertThat(e.getMessage(), Matchers.containsString("Corrupt content to decode"));
3131
}
3232
}
3333
}

ion/src/test/java/com/fasterxml/jackson/dataformat/ion/fuzz/Fuzz424_65065_65126NPETest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void testFuzz65065() throws Exception {
2323
MAPPER.readTree(new ByteArrayInputStream(bytes));
2424
fail("Should not pass (invalid content)");
2525
} catch (StreamReadException e) {
26-
assertThat(e.getMessage(), Matchers.containsString("Internal `IonReader` error"));
26+
assertThat(e.getMessage(), Matchers.containsString("Corrupt content to decode"));
2727
}
2828
}
2929

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fasterxml.jackson.dataformat.ion.fuzz;
2+
3+
import java.io.InputStream;
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.dataformat.ion.*;
11+
12+
import static org.hamcrest.MatcherAssert.assertThat;
13+
import static org.junit.Assert.fail;
14+
15+
// [dataformats-binary#4432]
16+
public class Fuzz432_65273_AssertionTest
17+
{
18+
private final IonFactory factory =
19+
IonFactory.builderForTextualWriters().build();
20+
21+
@Test
22+
public void testFuzz65273() throws Exception {
23+
try (InputStream in = getClass().getResourceAsStream("/data/fuzz-65273.ion")) {
24+
try (JsonParser p = factory.createParser(in)) {
25+
p.nextToken();
26+
}
27+
fail("Should not pass (invalid content)");
28+
} catch (StreamReadException e) {
29+
assertThat(e.getMessage(), Matchers.containsString("Corrupt content to decode; underlying"));
30+
}
31+
}
32+
}

ion/src/test/java/com/fasterxml/jackson/dataformat/ion/fuzz/Fuzz_65062_VarintTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void testFuzz65062_Varint() throws Exception {
3333
fail("Should not pass (invalid content)");
3434
} catch (StreamReadException e) {
3535
// 21-Dec-2023, tatu: Not 100% sure why we won't get Number-specific fail but:
36-
assertThat(e.getMessage(), Matchers.containsString("Corrupt content to decode; underlying failure"));
36+
assertThat(e.getMessage(), Matchers.containsString("Corrupt content to decode; underlying"));
3737
}
3838
}
3939
}

ion/src/test/resources/data/fuzz-65273.ion

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$ion_symbol_table::{imports:$0///// ����t

release-notes/CREDITS-2.x

+3
Original file line numberDiff line numberDiff line change
@@ -292,3 +292,6 @@ Arthur Chan (@arthurscchan)
292292
(2.17.0)
293293
* Contributed #426: (smile) `SmileParser` throws unexpected IOOBE for corrupt content
294294
(2.17.0)
295+
* Contributed #432 (ion) More methods from `IonReader` could throw an unexpected
296+
`AssertionError`
297+
(2.17.0)

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Active maintainers:
2626
(fix contributed by Arthur C)
2727
#426: (smile) `SmileParser` throws unexpected IOOBE for corrupt content
2828
(fix contributed by Arthur C)
29+
#432: (ion) More methods from `IonReader` could throw an unexpected `AssertionError`
30+
(fix contributed by Arthur C)
2931
-(ion) Update `com.amazon.ion:ion-java` to 1.11.0 (from 1.10.5)
3032

3133
2.16.1 (24-Dec-2023)

0 commit comments

Comments
 (0)