Skip to content

Make stream failures sticky in LZ4FrameInputStream and LZ4BlockInputStream - #145

Merged
yawkat merged 3 commits into
mainfrom
fix/issue-101
Sep 25, 2026
Merged

yawkat merged 3 commits into
mainfrom
fix/issue-101

Conversation

@yawkat

@yawkat yawkat commented Sep 25, 2026

Copy link
Copy Markdown
Owner

Fixes #101.

If a caller caught an IOException from either stream and kept reading, the stream could return data it had already rejected, or throw unchecked exceptions:

  • Frame stream: a frame whose header checksum failed was decoded anyway. A block with a failed checksum or decompression was silently dropped. A skippable frame followed by a corrupt header led to an NPE.
  • Block stream: a retry returned the bytes of a block whose checksum failed. available() could go negative, and read(byte[]) could throw IndexOutOfBoundsException.

Both classes now record the first failure. Any IOException or RuntimeException that escapes refill, nextFrameInfo or readBlock is stored, with RuntimeException wrapped in IOException. After that:

  • read(), read(byte[],int,int), skip() and the frame stream's getExpectedContentSize()/isExpectedContentSizeDefined() throw IOException("Stream previously failed", cause). This check runs before any EOF early return.
  • available() returns 0, and close() still works.
  • The Javadoc of both classes describes this.

Behaviour change (release note): once any read, skip or header parse fails, the stream stays failed. That includes retrying after a transient error from the underlying stream, such as SocketTimeoutException. Such retries were already unsafe, because partially read input had been consumed.

Tests (they fail without the fix):

  • Block stream: a corrupt block checksum, and an invalid originalLen (-1 and MAX_VALUE).
  • Frame stream: a corrupt second-frame descriptor, a skippable frame followed by a corrupt header, a block checksum mismatch, the content-size getters after a failure, and a malformed first header surfacing as IOException.

Merge note: this may conflict with #128 (refill() header handling) and #133 (tests appended at the same place). The resolution is to keep both: #128's header logic goes at the start of refill0().

🤖 Generated with Claude Code

…tream

After an IOException from reading a block or frame header, both streams
could keep returning data they had already rejected, or throw unchecked
exceptions (NullPointerException, IndexOutOfBoundsException). Record the
first failure, wrapping RuntimeExceptions in an IOException, and make
later read(), skip() and the frame stream's expected content size
methods throw an IOException caused by it. available() returns 0 once
the stream has failed.

Fixes #101

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@yawkat yawkat added this to the 1.12.0 milestone Sep 25, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@yawkat
yawkat enabled auto-merge (squash) September 25, 2026 19:03
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 87.17949% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.43%. Comparing base (f6e3304) to head (61dbbb7).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/java/net/jpountz/lz4/LZ4BlockInputStream.java 80.00% 3 Missing ⚠️
src/java/net/jpountz/lz4/LZ4FrameInputStream.java 91.66% 2 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@             Coverage Diff              @@
##               main     #145      +/-   ##
============================================
+ Coverage     79.76%   80.43%   +0.67%     
- Complexity      562      576      +14     
============================================
  Files            42       42              
  Lines          1838     1876      +38     
  Branches        247      253       +6     
============================================
+ Hits           1466     1509      +43     
  Misses          239      239              
+ Partials        133      128       -5     

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

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@yawkat
yawkat merged commit b98ff90 into main Sep 25, 2026
3 checks passed
@yawkat
yawkat deleted the fix/issue-101 branch September 25, 2026 19:14
dongjoon-hyun added a commit to apache/spark that referenced this pull request Sep 28, 2026
### What changes were proposed in this pull request?

This PR aims to upgrade `at.yawk.lz4:lz4-java` to 1.12.0.

### Why are the changes needed?

To bring the latest security fixes of `v1.11.4` and the stricter input validation of `v1.12.0`. The upstream recommends `1.12.0` over `1.11.4`.

- https://github.com/yawkat/lz4-java/releases/tag/v1.12.0 (2026-09-25)
  - [Throw IOException for invalid or unsupported frame descriptors](yawkat/lz4-java#133)
  - [Reject short output in LZ4DecompressorWithLength safe paths](yawkat/lz4-java#126)
  - [Make stream failures sticky in LZ4FrameInputStream and LZ4BlockInputStream](yawkat/lz4-java#145)
- https://github.com/yawkat/lz4-java/releases/tag/v1.11.4 (2026-09-25)
  - [LZ4FrameInputStream reallocates block buffers for every frame, allowing CPU and GC amplification from small inputs](GHSA-gm45-99xc-r7wv)
  - [LZ4BlockInputStream with stopOnEmptyBlock=false recurses once per empty block, causing StackOverflowError](GHSA-343h-94h5-c4wr)
  - [Native library extraction to a shared temporary directory is vulnerable to file replacement by another local user](GHSA-mcr4-qmvw-px4g)

Note that Apache Spark's `LZ4CompressionCodec` reads streams via `LZ4BlockInputStream` with `withStopOnEmptyBlock(false)`, which is the code path fixed by `GHSA-343h-94h5-c4wr`.

**Full Changelog**: yawkat/lz4-java@v1.11.3...v1.12.0

### Does this PR introduce _any_ user-facing change?

No. There is no behavior change for valid LZ4 streams.

### How was this patch tested?

Pass the CIs.

### Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Opus 5.5

Closes #59093 from dongjoon-hyun/SPARK-59820.

Authored-by: Dongjoon Hyun <dongjoon@apache.org>
Signed-off-by: Dongjoon Hyun <dongjoon@apache.org>
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.

LZ4FrameInputStream / LZ4BlockInputStream keep returning data after a failed read

2 participants