Skip to content

Handle zero-length and invalid bulk reads in ByteBufInputStream - #18272

Merged
normanmaurer merged 2 commits into
netty:4.2from
wozhenbang2004:fix/bytebuf-input-bulk-read
Nov 12, 2026
Merged

normanmaurer merged 2 commits into
netty:4.2from
wozhenbang2004:fix/bytebuf-input-bulk-read

Conversation

@wozhenbang2004

Copy link
Copy Markdown
Contributor

Motivation:

ByteBufInputStream.read(byte[], int, int) returns -1 for a zero-length read when the stream is exhausted. It also checks the destination range only after limiting the read to the available bytes, so an invalid range can be accepted at EOF or hidden by a short read.

Modification:

Validate the original destination range using the existing ObjectUtil and MathUtil helpers, then return 0 for a zero-length request before checking EOF. Add regressions for empty and consumed streams and invalid destination ranges.

Result:

Bulk reads follow the InputStream contract. This adds validation for invalid ranges that previously escaped checking, and reports negative lengths as IndexOutOfBoundsException.

ByteBufStreamTest executes 14 tests. The four new parameterized cases fail on the unchanged implementation and pass with the fix. The targeted Maven build and Checkstyle checks pass on JDK 21 with Java 8 release compilation.

Revapi reports no API compatibility failures against 4.2.20.Final. The legacy forbidden-API checker reports JDK 21 as unsupported, so that check remains for CI.

@testlens-app

testlens-app Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 3c890ca
▶️ Tests: 77177 executed
⚪️ Checks: 33/33 completed


Learn more about TestLens at testlens.app/docs.

@normanmaurer normanmaurer added this to the 4.2.21.Final milestone Nov 9, 2026
@normanmaurer normanmaurer added needs-cherry-pick-4.1 This PR should be cherry-picked to 4.1 once merged. needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. labels Nov 12, 2026
@normanmaurer
normanmaurer merged commit ec663d4 into netty:4.2 Nov 12, 2026
29 checks passed
@netty-project-bot

Copy link
Copy Markdown
Contributor

Auto-port PR for 4.1: #18351

@netty-project-bot

Copy link
Copy Markdown
Contributor

Auto-port PR for 5.0: #18352

@github-actions github-actions Bot removed the needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. label Nov 12, 2026
normanmaurer added a commit that referenced this pull request Nov 12, 2026
The auto-port of #18272 used lambdas and try-with-resources, which do not
compile with the source level 4.1 uses by default (1.6 / 1.7 depending on the
JDK). Use anonymous Executable instances and try/finally instead, like the
rest of the test class.
normanmaurer pushed a commit that referenced this pull request Nov 12, 2026
…putStream (#18352)

Auto-port of #18272 to 5.0
Cherry-picked commit: ec663d4

---
Motivation:

`ByteBufInputStream.read(byte[], int, int)` returns `-1` for a
zero-length read when the stream is exhausted. It also checks the
destination range only after limiting the read to the available bytes,
so an invalid range can be accepted at EOF or hidden by a short read.

Modification:

Validate the original destination range using the existing `ObjectUtil`
and `MathUtil` helpers, then return `0` for a zero-length request before
checking EOF. Add regressions for empty and consumed streams and invalid
destination ranges.

Result:

Bulk reads follow the `InputStream` contract. This adds validation for
invalid ranges that previously escaped checking, and reports negative
lengths as `IndexOutOfBoundsException`.

`ByteBufStreamTest` executes 14 tests. The four new parameterized cases
fail on the unchanged implementation and pass with the fix. The targeted
Maven build and Checkstyle checks pass on JDK 21 with Java 8 release
compilation.

Revapi reports no API compatibility failures against 4.2.20.Final. The
legacy forbidden-API checker reports JDK 21 as unsupported, so that
check remains for CI.

Co-authored-by: Kayokoi <[email protected]>
normanmaurer added a commit that referenced this pull request Nov 12, 2026
…putStream (#18351)

Auto-port of #18272 to 4.1
Cherry-picked commit: ec663d4

---
Motivation:

`ByteBufInputStream.read(byte[], int, int)` returns `-1` for a
zero-length read when the stream is exhausted. It also checks the
destination range only after limiting the read to the available bytes,
so an invalid range can be accepted at EOF or hidden by a short read.

Modification:

Validate the original destination range using the existing `ObjectUtil`
and `MathUtil` helpers, then return `0` for a zero-length request before
checking EOF. Add regressions for empty and consumed streams and invalid
destination ranges.

Result:

Bulk reads follow the `InputStream` contract. This adds validation for
invalid ranges that previously escaped checking, and reports negative
lengths as `IndexOutOfBoundsException`.

`ByteBufStreamTest` executes 14 tests. The four new parameterized cases
fail on the unchanged implementation and pass with the fix. The targeted
Maven build and Checkstyle checks pass on JDK 21 with Java 8 release
compilation.

Revapi reports no API compatibility failures against 4.2.20.Final. The
legacy forbidden-API checker reports JDK 21 as unsupported, so that
check remains for CI.

---------

Co-authored-by: Kayokoi <[email protected]>
Co-authored-by: Norman Maurer <[email protected]>
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