Skip to content

Commit

Permalink
Add null checking and wrap NPE
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Chan <[email protected]>
  • Loading branch information
arthurscchan committed Dec 18, 2023
1 parent db12a65 commit b8b5c19
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,13 @@ public String getText() throws IOException
msg = "UNKNOWN ROOT CAUSE";
}
throw _constructError("Internal `IonReader` error: "+msg, e);
} catch (NullPointerException e) {
// NullPointerException could be thrown on invalid data
String msg = e.getMessage();
if (msg == null) {
msg = "UNKNOWN ROOT CAUSE";
}
throw _constructError("Internal `IonReader` error: "+msg, e);
}
case VALUE_NUMBER_INT:
case VALUE_NUMBER_FLOAT:
Expand Down Expand Up @@ -331,31 +338,55 @@ public int getTextOffset() throws IOException {

@Override
public BigInteger getBigIntegerValue() throws IOException {
NumberType nt = getNumberType();
if (nt == null) {
throw _constructError("Wrong number type: BigInteger not found.", null);
}
return _reader.bigIntegerValue();
}

@Override
public BigDecimal getDecimalValue() throws IOException {
NumberType nt = getNumberType();
if (nt == null) {
throw _constructError("Wrong number type: BigDecimal not found.", null);
}
return _reader.bigDecimalValue();
}

@Override
public double getDoubleValue() throws IOException {
NumberType nt = getNumberType();
if (nt == null) {
throw _constructError("Wrong number type: Double not found.", null);
}
return _reader.doubleValue();
}

@Override
public float getFloatValue() throws IOException {
NumberType nt = getNumberType();
if (nt == null) {
throw _constructError("Wrong number type: Float not found.", null);
}
return (float) _reader.doubleValue();
}

@Override
public int getIntValue() throws IOException {
NumberType nt = getNumberType();
if (nt == null) {
throw _constructError("Wrong number type: Int not found.", null);
}
return _reader.intValue();
}

@Override
public long getLongValue() throws IOException {
NumberType nt = getNumberType();
if (nt == null) {
throw _constructError("Wrong number type: Long not found.", null);
}
return _reader.longValue();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.fasterxml.jackson.dataformat.ion.fuzz;

import java.io.ByteArrayInputStream;

import org.hamcrest.Matchers;
import org.junit.Test;

import com.fasterxml.jackson.core.exc.StreamReadException;
import com.fasterxml.jackson.dataformat.ion.*;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.fail;

// [dataformats-binary#424]
public class Fuzz424_65065_65126NPETest
{
@Test
public void testFuzz65065() throws Exception {
IonFactory f = IonFactory
.builderForBinaryWriters()
.build();

IonObjectMapper mapper = IonObjectMapper.builder(f).build();

try {
byte[] bytes = {(byte) -32, (byte) 1, (byte) 0, (byte) -22, (byte) 123, (byte) -112};
mapper.readTree(f.createParser(new ByteArrayInputStream(bytes)));
fail("Should not pass (invalid content)");
} catch (StreamReadException e) {
assertThat(e.getMessage(), Matchers.containsString("UNKNOWN ROOT CAUSE"));
}
}

@Test
public void testFuzz65126() throws Exception {
IonFactory f = IonFactory
.builderForBinaryWriters()
.build();

try {
byte[] bytes = {(byte) 1, (byte) 0};
f.createParser(bytes).getDecimalValue();
fail("Should not pass (invalid content)");
} catch (StreamReadException e) {
assertThat(e.getMessage(), Matchers.containsString("Wrong number type"));
}
}
}

0 comments on commit b8b5c19

Please sign in to comment.