Skip to content

Fix readNBytes single-byte path treating 0xFF as EOF#30357

Draft
wenshao wants to merge 1 commit intoopenjdk:masterfrom
wenshao:fix-readnbytes-0xff-eof
Draft

Fix readNBytes single-byte path treating 0xFF as EOF#30357
wenshao wants to merge 1 commit intoopenjdk:masterfrom
wenshao:fix-readnbytes-0xff-eof

Conversation

@wenshao
Copy link
Copy Markdown
Contributor

@wenshao wenshao commented Mar 22, 2026

Summary

FileInputStream.readNBytes() and ChannelInputStream.readNBytes() have a single-byte retry path (entered when read(byte[],int,int) returns 0) that casts read() to byte before comparing with -1:

byte b = (byte)read();
if (b == -1) break;

Since (byte)255 == -1 in Java's signed byte arithmetic, the byte value 0xFF is incorrectly interpreted as EOF. This causes readNBytes() to return truncated data when a 0xFF byte is encountered in this code path.

Fix

Store the read() result as int (range -1 to 255) and cast to byte only after the EOF check:

int b = read();
if (b == -1) break;
buf[nread++] = (byte) b;

Both FileInputStream.java and ChannelInputStream.java have the identical bug and are fixed the same way.

Test plan

  • New test ReadNBytesOxFF.java creates a file filled with 0xFF bytes and verifies readNBytes() returns the complete data without truncation, for both FileInputStream and ChannelInputStream

Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/30357/head:pull/30357
$ git checkout pull/30357

Update a local copy of the PR:
$ git checkout pull/30357
$ git pull https://git.openjdk.org/jdk.git pull/30357/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 30357

View PR using the GUI difftool:
$ git pr show -t 30357

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/30357.diff

FileInputStream.readNBytes() and ChannelInputStream.readNBytes() have
a single-byte retry path that is entered when read(byte[],int,int)
returns 0. This path casts read() to byte before comparing with -1:

    byte b = (byte)read();
    if (b == -1) break;

Since (byte)255 == -1 in Java's signed byte arithmetic, the byte
value 0xFF is incorrectly interpreted as EOF, causing readNBytes()
to return truncated data.

Fix by storing the read() result as int (range -1 to 255) and
casting to byte only after the EOF check:

    int b = read();
    if (b == -1) break;
    buf[nread++] = (byte) b;
@bridgekeeper
Copy link
Copy Markdown

bridgekeeper bot commented Mar 22, 2026

👋 Welcome back swen! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link
Copy Markdown

openjdk bot commented Mar 22, 2026

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk openjdk bot added nio nio-dev@openjdk.org core-libs core-libs-dev@openjdk.org labels Mar 22, 2026
@openjdk
Copy link
Copy Markdown

openjdk bot commented Mar 22, 2026

@wenshao The following labels will be automatically applied to this pull request:

  • core-libs
  • nio

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core-libs core-libs-dev@openjdk.org nio nio-dev@openjdk.org

Development

Successfully merging this pull request may close these issues.

1 participant