Skip to content

Reject symbol IDs and VarInts that overflow int in the binary reader - #1173

Merged
tgregg merged 5 commits into
amazon-ion:masterfrom
radhika1314:binary-reader-int-overflow
Dec 4, 2026
Merged

tgregg merged 5 commits into
amazon-ion:masterfrom
radhika1314:binary-reader-int-overflow

Conversation

@radhika1314

Copy link
Copy Markdown
Contributor

Issue #, if available: n/a

Description of changes:

The Ion 1.0 binary reader decodes VarUInt and VarInt sub-fields into a Java int without checking that they fit, and narrows field name and symbol value IDs from long with a plain cast. A 5-byte VarUInt of 2^32 + 4 wraps to 4, so an out-of-range symbol ID used as a field name, annotation or symbol value resolves to $4 (name) instead of failing the way fieldNameSymbolIDUnmapped.10n and annotationSymbolIDUnmapped.10n in ion-tests require. The same wrap turns a timestamp year of 2^32 + 2000 into 2000T, and a decimal exponent of 2^31 + 1 into -(2^31 - 1), so 1d2147483649 reads back as 1d-2147483647. Two Ion implementations can end up disagreeing about the contents of the same document.

Reproducer for the field name case (the others are covered by the new tests):

e0 01 00 ea d7 10 00 00 00 84 21 01

Before: {name: 1}. After: IonException: Found a symbol ID that was too large to fit in an int.

The checks live in the sub-field readers so that every caller (field names, annotations, symbol values, timestamp fields, decimal exponents) is covered in one place: readVarUInt_1_0 bounds its accumulator, readVarInt_1_0 accumulates in a long with a 2^31 magnitude bound and the decimal scale and timestamp offset are range checked before narrowing, and IonCursorBinary.symbolIdToInt rejects symbol IDs outside the int range. The VarInt bound still admits an exponent of exactly 2^31, so a BigDecimal with scale Integer.MIN_VALUE remains readable.

One side effect worth a look: IonRawBinaryWriter.writeDecimalValue computes the exponent as -value.scale() in int arithmetic, so a scale of Integer.MIN_VALUE is written as exponent -2^31 and only round tripped because the reader overflowed the same way. That encoding now fails to read back. I kept this change on the read side, but can fold the writer fix in here if you would rather have both together.

Regression tests were added to IonReaderContinuableCoreBinaryTest and IonReaderContinuableTopLevelBinaryTest; ./gradlew build and minifyTest21 pass locally.

@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.84848% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 68.40%. Comparing base (3c1b6b1) to head (b6bacf5).
⚠️ Report is 175 commits behind head on master.

Files with missing lines Patch % Lines
...main/java/com/amazon/ion/impl/IonCursorBinary.java 66.66% 1 Missing and 2 partials ⚠️
...mazon/ion/impl/IonReaderContinuableCoreBinary.java 90.47% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1173      +/-   ##
============================================
+ Coverage     67.23%   68.40%   +1.17%     
- Complexity     5484     5768     +284     
============================================
  Files           159      160       +1     
  Lines         23025    23543     +518     
  Branches       4126     4251     +125     
============================================
+ Hits          15481    16105     +624     
+ Misses         6262     6134     -128     
- Partials       1282     1304      +22     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tgregg tgregg left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

-2^31 and only round tripped because the reader overflowed the same way. That encoding now fails to read back. I kept this change on the read side, but can fold the writer fix in here if you would rather have both together.

Yes, it would be great to include the write side fix here too.

tgregg and others added 3 commits December 4, 2026 00:15
…in the binary writer

- symbolValueId now skips leading zero bytes before checking that the
  symbol ID fits in an int, so zero-padded IDs that fit are accepted.
- IonRawBinaryWriter computes the decimal (and fractional second)
  exponent as -(long) scale, so a scale of Integer.MIN_VALUE is written
  as exponent 2^31 rather than overflowing to -2^31.

Co-authored-by: Radhika <radhika@research.itecz.au>
IonDatagram.getBytes() negated the decimal scale in int arithmetic, and
writeVarInt(int) mis-encoded Integer.MIN_VALUE as -0. A BigDecimal with
scale Integer.MIN_VALUE was therefore silently written as exponent -0
(e.g. 1d2147483648 round tripped as 1d0). The exponent is now computed
as a long and writeVarInt accepts a long magnitude up to 2^31.

Co-authored-by: Radhika <radhika@research.itecz.au>

@NovemberZulu NovemberZulu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Unhugged else feels lonely

@tgregg
tgregg merged commit 963b3a2 into amazon-ion:master Dec 4, 2026
32 of 38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants