Skip to content

[Bug] Fix UnsafeByteArrayInputStream to return 0 for zero-length reads at EOF - #16556

Open
siddiqueirshad wants to merge 2 commits into
apache:3.3from
siddiqueirshad:fix/unsafe-byte-array-zero-read
Open

siddiqueirshad wants to merge 2 commits into
apache:3.3from
siddiqueirshad:fix/unsafe-byte-array-zero-read

Conversation

@siddiqueirshad

Copy link
Copy Markdown

Description

According to the \InputStream.read(byte[], int, int)\ contract, if \len\ is zero, 0 should be returned without modifying destination buffers or advancing position, even when the stream is exhausted or empty.
Previously, the EOF check ran before the zero-length check in \UnsafeByteArrayInputStream.read(byte[], int, int), causing
ead(b, off, 0)\ and
ead(new byte[0])\ to return -1 at EOF.

This PR:

  • Handles \len == 0\ after argument validation and before the EOF check.
  • Adds unit tests in \UnsafeByteArrayInputStreamTest\ covering zero-length reads on empty and exhausted streams.

Fixes #16554

…s at EOF

According to the InputStream.read(byte[], int, int) contract, if len is zero, then 0 should be returned without modifying destination buffers or advancing position, even when the stream is exhausted or empty.
Previously, the EOF check ran before the zero-length check, causing read(b, off, 0) and read(new byte[0]) to return -1 at EOF.

Handle len == 0 after argument validation and before the EOF check. Add unit tests covering zero-length reads in UnsafeByteArrayInputStreamTest.

Fixes apache#16554

Signed-off-by: siddiqui irshad <mohdirshad1306@gmail.com>
@codecov-commenter

codecov-commenter commented Oct 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 61.01%. Comparing base (1afcdf7) to head (0dc4a16).
⚠️ Report is 5 commits behind head on 3.3.

Additional details and impacted files
@@             Coverage Diff              @@
##                3.3   #16556      +/-   ##
============================================
+ Coverage     59.02%   61.01%   +1.98%     
  Complexity       15       15              
============================================
  Files          1953     1953              
  Lines         89284    89286       +2     
  Branches      13477    13478       +1     
============================================
+ Hits          52700    54476    +1776     
+ Misses        30877    29207    -1670     
+ Partials       5707     5603     -104     
Flag Coverage Δ
integration-tests-java21 32.10% <0.00%> (?)
integration-tests-java8 32.20% <0.00%> (?)
samples-tests-java21 32.10% <0.00%> (?)
samples-tests-java8 29.75% <0.00%> (?)
unit-tests-java11 59.26% <100.00%> (+0.04%) ⬆️
unit-tests-java17 58.71% <100.00%> (+0.04%) ⬆️
unit-tests-java21 58.74% <100.00%> (+0.06%) ⬆️
unit-tests-java25 58.66% <100.00%> (-0.01%) ⬇️
unit-tests-java8 59.27% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@LI123456mo LI123456mo 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.

This PR #16555 but Your recommended fix is solid too, the only thing thing that lacks in your PR is making sure the tests covers the old behaviour compared to this #16555 which covers all

@siddiqueirshad

Copy link
Copy Markdown
Author

Hi @LI123456mo, thanks for taking a look! Since #16555 covers the comprehensive test suite for both legacy and edge behaviors across the stream reads, I am happy to close this PR in favor of #16555 so efforts remain consolidated. Thanks again for the review!

Signed-off-by: siddiqueirshad <mohdirshad1306@gmail.com>
@siddiqueirshad

Copy link
Copy Markdown
Author

Updated the test method \ estReadZeroLength()\ to declare \ hrows IOException\ to fix the compilation error across JDK 8/11/17/21/25.

This branch has not been deployed

No deployments
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.

[Bug] UnsafeByteArrayInputStream returns EOF for zero-length reads

3 participants