Skip to content

Reject symbol table imports whose max_id overflows int - #1177

Merged
tgregg merged 2 commits into
amazon-ion:masterfrom
radhika1314:symtab-import-max-id-overflow
Dec 4, 2026
Merged

tgregg merged 2 commits into
amazon-ion:masterfrom
radhika1314:symtab-import-max-id-overflow

Conversation

@radhika1314

Copy link
Copy Markdown
Contributor

Issue #, if available: n/a

Description of changes:

LocalSymbolTableImports.prepBaseSids adds up the imports' max_id values in an int to find where the local symbols start, with no range check. The text reader, the DOM loader and the binary reader all build their imports through it, so a document can pick max_ids whose sum wraps and put its local symbols on IDs that belong to the system symbol table or to an import. With the 9 system symbols, these two imports add up to 2^32, which wraps to 0:

$ion_symbol_table::{
  imports:[{name:"a", version:1, max_id:2147483647},
           {name:"b", version:1, max_id:2147483640}],
  symbols:["x", "y", "z", "role"]
}
{$4: 1}

Before: {role:1} from all three readers (the binary case was checked with the same table encoded by hand). $4 is the system symbol name whatever the imports declare. After: IonException: Symbol table imports have a combined max_id that is too large to fit in an int.

Every individual value here fits in an int, so this is separate from #1173, which bounds VarUInt/VarInt decoding and symbol ID narrowing.

Changes:

  1. prepBaseSids rejects a combined max_id that leaves no int for the first local SID (total + 1). A total of exactly Integer.MAX_VALUE is rejected too; today it turns the first local SID into Integer.MIN_VALUE, and the binary reader then reports $4 as unknown.
  2. The binary reader's readImportMaxId, and LocalSymbolTable.readOneImport when it runs over a tree reader, narrow max_id with intValue(), so max_id: 4294967305 becomes 9 and the local symbols start inside the import's range. The text reader already rejects that value. Both now range check before narrowing. I used bigIntegerValue().bitLength() instead of getIntegerSize() because the binary reader sizes integers from their encoded length, which would reject a zero-padded small max_id.

Tests: testImportsWithMaxIdLargerThanInt in SymbolTableTest (every reader maker, including a check that the largest accepted import range still loads) and two cases in IonReaderContinuableTopLevelBinaryTest; all of them fail without the fix. ./gradlew build and minifyTest21 pass locally. Spotless rewrote the license header on the three files that still had the long form.

Something I noticed but left out: with the imports ending at $2147483646, the binary reader still can't resolve a local symbol at exactly $2147483647, because getSymbolToken computes localSymbolMaxOffset + firstLocalSymbolId + 1 and that overflows. It fails closed with UnknownSymbolException, so it seemed like a separate change. Happy to fold it in if you'd rather.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@radhika1314

Copy link
Copy Markdown
Contributor Author

gentle ping

@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 75.00000% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 68.42%. Comparing base (3c1b6b1) to head (db36d11).
⚠️ Report is 178 commits behind head on master.

Files with missing lines Patch % Lines
.../main/java/com/amazon/ion/impl/_Private_Utils.java 33.33% 4 Missing and 2 partials ⚠️
...on/impl/IonReaderContinuableApplicationBinary.java 80.00% 1 Missing and 2 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1177      +/-   ##
============================================
+ Coverage     67.23%   68.42%   +1.18%     
- Complexity     5484     5775     +291     
============================================
  Files           159      160       +1     
  Lines         23025    23572     +547     
  Branches       4126     4258     +132     
============================================
+ Hits          15481    16129     +648     
+ Misses         6262     6136     -126     
- Partials       1282     1307      +25     

☔ 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.

radhika1314 and others added 2 commits December 4, 2026 09:08
The first local symbol ID is the sum of the imports' max_id values,
accumulated in an int by LocalSymbolTableImports.prepBaseSids. Imports
of max_id 2147483647 and 2147483640 wrap the total to 0, so local
symbols land on the system symbol IDs in the text, DOM and binary
readers. Reject a combined max_id that leaves no int-sized first local
ID, and range check max_id before narrowing it in both import parsers.
…nd reject local symbol counts that overflow max_id

- Read import version and max_id via a helper that checks getIntegerSize() and only
  allocates a BigInteger when the value may not fit in a long.
- Reject a version that does not fit in an int instead of silently narrowing it.
- Reject symbol tables whose imports' max_id plus local symbol count overflows int,
  in both the binary reader and LocalSymbolTable.
- Avoid overflow in the binary reader's getSymbolToken when the max ID is Integer.MAX_VALUE.
@tgregg
tgregg force-pushed the symtab-import-max-id-overflow branch from dad08e6 to db36d11 Compare December 4, 2026 17:08
@tgregg
tgregg merged commit b6c6677 into amazon-ion:master Dec 4, 2026
28 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