Skip to content

Restore IonException for malformed text input - #1185

Merged
junyans merged 3 commits into
amazon-ion:masterfrom
junyans:fix-malformed-text-input-exception
Dec 2, 2026
Merged

junyans merged 3 commits into
amazon-ion:masterfrom
junyans:fix-malformed-text-input-exception

Conversation

@junyans

@junyans junyans commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Issue #, if available: N/A — regression in v1.12.2 from #1178.

Description of changes:

Problem

In 1.12.2, when the text reader fails to decode a malformed blob, clob or quoted field name and is then closed, the caller gets an internal error instead of the IonException:

  • with assertions enabled (e.g. Gradle tests): java.lang.AssertionError at UnifiedSavePointManagerX$SavePoint.clear(UnifiedSavePointManagerX.java:318)
  • without assertions: IllegalArgumentException: you can't release an active save point

IonSystem.singleValue(byte[]) and IonLoader.load(byte[]) close the reader in a finally block, so they hit this on input like {a:{{invalid_blob}}}:

java.lang.AssertionError
  at com.amazon.ion.impl.UnifiedSavePointManagerX$SavePoint.clear(UnifiedSavePointManagerX.java:318)
  at com.amazon.ion.impl.IonReaderTextRawX.clear_current_value_buffer(IonReaderTextRawX.java:389)
  at com.amazon.ion.impl.IonReaderTextRawX.clear_value(IonReaderTextRawX.java:535)
  at com.amazon.ion.impl.IonReaderTextRawX.close(IonReaderTextRawX.java:368)
  at com.amazon.ion.impl.lite.IonSystemLite.singleValue(IonSystemLite.java:392)

Bisect: v1.12.1 and ab9cfeab~1 throw IonException; ab9cfeab (#1178) throws AssertionError.

Cause

IonReaderTextSystemX.load_lob_contents() and IonReaderTextRawX.token_contents_load() activate the value's save point, decode it, then deactivate it. A decode error skipped the deactivation, leaving the save point active. #1178 made close() call clear_value(), which clears that still-active save point, and the error thrown from close() replaces the IonException. The same leftover state also made next() or a second newBytes() after the error fail with AssertionError, already in 1.12.1.

Change

Tests

  • IonReaderCloseableTest.testTextReaderClosesAfterMalformedValue: malformed lobs and quoted field names from String and byte[]; close() succeeds and next() then returns null; singleValue(byte[]) throws the same IonException message as reading without closing.
  • IonReaderTextFastPathsTest.malformedValuesLeaveTheReaderUsable: for every Source (bytes, streams including 1- and 4093-byte reads, string, reader), including values longer than a page: reading the malformed value throws IonException, reading it again throws IonException, the reader moves on to the next value, and close() succeeds.
  • BadIonTest.testLoadBytes: loads the ion-tests bad corpus through load(byte[]), which closes the reader. The existing load(File) and load(String) paths do not close it, which is why the corpus did not catch this regression. 58 of its 986 cases fail without the fix.
  • Formatting: spotless moved the two touched test files to the SPDX header and removed an unused import.

Each new test fails without the main-source change, and reverting either of the two finally blocks alone fails the tests again. ./gradlew check passes: 199,645 tests, 0 failures, 586 skipped.

Not addressed here (pre-existing in 1.12.1; to be filed separately)

  • If skip_over_lob itself throws (e.g. {{abcd}x), a second newBytes() fails an assertion in load_lob_save_point.
  • A valid triple-quoted clob ({{'''abc'''}}) read from a stream that returns a few bytes per read throws IllegalArgumentException: end point ... must be within 1 page.
  • After a string value fails to load ({a:"x\q", b:"yz"}), the next field name is read as xb.

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

@codecov

codecov Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 47.82609% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 68.38%. Comparing base (3c1b6b1) to head (4cdeb86).
⚠️ Report is 169 commits behind head on master.

Files with missing lines Patch % Lines
...in/java/com/amazon/ion/impl/IonReaderTextRawX.java 42.85% 8 Missing and 4 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1185      +/-   ##
============================================
+ Coverage     67.23%   68.38%   +1.14%     
- Complexity     5484     5738     +254     
============================================
  Files           159      160       +1     
  Lines         23025    23497     +472     
  Branches       4126     4232     +106     
============================================
+ Hits          15481    16068     +587     
+ Misses         6262     6129     -133     
- Partials       1282     1300      +18     

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

Since closing a text reader started clearing its current value (amazon-ion#1178),
a lob or quoted field name that failed to decode surfaced as an
AssertionError, or an IllegalArgumentException without -ea, hiding the
IonException callers handle. Leaving the reader as it was before the
failed read also lets it read on or be retried.
@junyans
junyans force-pushed the fix-malformed-text-input-exception branch from eb91793 to 62809ac Compare December 1, 2026 06:55
Comment on lines +340 to +341
// A triple-quoted clob read a byte at a time fails even when it is valid, a separate problem.
lobs.add("{{'''x\\q'''}} 7");

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.

Please open an issue

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Opened the issue #1187

Comment on lines +62 to +68
byte[] bytes = _Private_Utils.loadFileBytes(myTestFile);
try
{
loader().load(bytes);
fail("Expected IonException");
}
catch (IonException e) { /* good */ }

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.

Rephrase using assertThrows?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done

Comment on lines +174 to +179
try {
system.getLoader().load(reader);
fail("Expected IonException for " + ion);
} catch (IonException e) {
message = e.getMessage();
}

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.

assertThrows

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done

Comment on lines +180 to +184
try {
reader.close();
} catch (Throwable t) {
throw new AssertionError("close() after the IonException for " + ion + " threw " + t, t);
}

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.

This catch is unnecessary, as an exception thrown from close() here will fail the test (as desired).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Removed

Comment on lines +190 to +195
try {
system.singleValue(bytes);
fail("Expected IonException for " + ion);
} catch (IonException e) {
assertEquals("IonException from singleValue(byte[]) for " + ion, message, e.getMessage());
}

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.

assertThrows

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done

The hand-rolled checks obscured what each test expects, and an
exception from close() already fails the test without extra handling.

Addresses review on amazon-ion#1185
So the case the test leaves out points at the issue that tracks it
instead of an unexplained note.

Addresses review on amazon-ion#1185
@junyans

junyans commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator Author

@tgregg I ran the benchmarks from #1172's description against master (09551a7) and this branch (4cdeb86). I don't see a difference: for every pair the before and after confidence intervals overlap, and no change comes close to the 9–30% that #1172 gained.

Setup. ion-java-benchmark-cli with --format ion_text --mode AverageTime --forks 5 --warmups 2 --iterations 3 --io-type buffer, the six datasets generated as in ion-java-performance-regression-detector.yml (-S 50000), JDK 17 (Corretto), one benchmark at a time on an otherwise idle 32-core aarch64 Linux host. The CLI was pinned to each side's ion-java; I used the shaded jar because ProGuard needs jmods, which this JDK doesn't have. Both sides were built the same way.

ms/op, lower is better, ± the 99.9% confidence interval:

Dataset Streaming before after change DOM before after change
nestedList 1.134 ± 0.047 1.165 ± 0.035 +2.7% 1.821 ± 0.013 1.823 ± 0.012 +0.1%
nestedStruct 1.946 ± 0.067 1.972 ± 0.103 +1.3% 3.269 ± 0.032 3.282 ± 0.028 +0.4%
sexp 0.422 ± 0.007 0.423 ± 0.005 +0.2% 0.720 ± 0.020 0.727 ± 0.025 +1.0%
realWorldDataSchema01 2.808 ± 0.094 2.983 ± 0.113 +6.2% 4.417 ± 0.247 4.445 ± 0.114 +0.6%
realWorldDataSchema02 7.632 ± 0.182 7.645 ± 0.083 +0.2% 11.896 ± 0.120 11.909 ± 0.127 +0.1%
realWorldDataSchema03 1.962 ± 0.014 1.947 ± 0.058 −0.8% 3.125 ± 0.047 3.113 ± 0.043 −0.4%

The streaming realWorldDataSchema01 pair was the only one near the edge, so I ran it again with the order reversed (after first): 2.862 ± 0.124 before, 2.939 ± 0.166 after (+2.7%).

Since these datasets have few large blobs or quoted field names, I also timed those two paths directly, in the same rough way as #1172 (best of 5 after 2 warm-ups, not JMH):

Case Before After Change
8 MiB blob, byte[] 21.8 ms 22.4 ms +2.8%
8 MiB blob, InputStream 27.3 ms 27.3 ms 0.0%
8 MiB blob, InputStream, base64 wrapped at 76 characters 28.9 ms 28.8 ms −0.3%
8 MiB blob, String 34.8 ms 34.7 ms −0.3%
8 MiB blob, Reader 35.1 ms 35.0 ms −0.3%
500k quoted field names, byte[] 128.1 ms 130.8 ms +2.1%
500k quoted field names, InputStream 138.7 ms 136.5 ms −1.6%

Run to run, the same build varied by up to about 8% in these.

That fits the bytecode: in load_lob_contents() the non-exceptional path is the same instructions before and after (activate, readBytes, deactivate, goto). The finally adds an exception handler and an exception-table entry, which only matter when a read throws.

@junyans
junyans merged commit 7c8ff17 into amazon-ion:master Dec 2, 2026
30 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.

2 participants