Skip to content

Handle value overflow checks for IonParser.getIntValue() - #428 #481

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

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,25 @@ public int getIntValue() throws IOException {
// @since 2.17
private int _getIntValue() throws IOException {
try {
return _reader.intValue();
} catch (IonException e) {
if (getNumberType() == NumberType.LONG) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: for performance reasons, I recommend storing this value and using it in both branches. ion-java has to do some bounds checking on the values, and we should avoid doing that twice when possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok will do. Thanks @tgregg

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was about to suggest same, +1

int result = _reader.intValue();
if ((long) result != _reader.longValue()) {
this.reportOverflowInt();
}
return result;
}
if (getNumberType() == NumberType.BIG_INTEGER) {
BigInteger bigInteger = _reader.bigIntegerValue();
if (BI_MIN_INT.compareTo(bigInteger) > 0 || BI_MAX_INT.compareTo(bigInteger) < 0) {
this.reportOverflowInt();
}
return bigInteger.intValue();
} else {
return _reader.intValue();
}
} catch (IonException
// 15-Jan-2024, tatu: other OSS-Fuzz tests suggest we need this:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not add this back (I think @tgregg is working on handling these edge cases)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. If fuzzing identifies any new leaked exceptions I'd like to fix those in ion-java; in the meantime we can cover them in failing tests.

| ArrayIndexOutOfBoundsException e) {
return _reportCorruptNumber(e);
}
}
Expand All @@ -405,8 +422,18 @@ public long getLongValue() throws IOException {
// @since 2.17
private long _getLongValue() throws IOException {
try {
return _reader.longValue();
} catch (IonException e) {
if (this.getNumberType() == NumberType.BIG_INTEGER) {
BigInteger bigInteger = _reader.bigIntegerValue();
if (BI_MIN_INT.compareTo(bigInteger) > 0 || BI_MAX_INT.compareTo(bigInteger) < 0) {
this.reportOverflowLong();
}
return bigInteger.longValue();
} else {
return _reader.longValue();
}
} catch (IonException
// 14-Jan-2024, tatu: OSS-Fuzz#65731 points to AIOOBE:
| ArrayIndexOutOfBoundsException e) {
return _reportCorruptNumber(e);
}
}
Expand Down