Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for #424: Add null checking and wrap NPE #425

Merged
merged 4 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
cowtowncoder marked this conversation as resolved.
Show resolved Hide resolved
// 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();
arthurscchan marked this conversation as resolved.
Show resolved Hide resolved
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("Internal `IonReader` error"));
}
}

@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"));
}
}
}