Fix readNBytes single-byte path treating 0xFF as EOF#30357
Draft
wenshao wants to merge 1 commit intoopenjdk:masterfrom
Draft
Fix readNBytes single-byte path treating 0xFF as EOF#30357wenshao wants to merge 1 commit intoopenjdk:masterfrom
wenshao wants to merge 1 commit intoopenjdk:masterfrom
Conversation
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;
|
👋 Welcome back swen! A progress list of the required criteria for merging this PR into |
|
❗ This change is not yet ready to be integrated. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
FileInputStream.readNBytes()andChannelInputStream.readNBytes()have a single-byte retry path (entered whenread(byte[],int,int)returns 0) that castsread()tobytebefore comparing with -1:Since
(byte)255 == -1in Java's signed byte arithmetic, the byte value0xFFis incorrectly interpreted as EOF. This causesreadNBytes()to return truncated data when a0xFFbyte is encountered in this code path.Fix
Store the
read()result asint(range -1 to 255) and cast tobyteonly after the EOF check:Both
FileInputStream.javaandChannelInputStream.javahave the identical bug and are fixed the same way.Test plan
ReadNBytesOxFF.javacreates a file filled with0xFFbytes and verifiesreadNBytes()returns the complete data without truncation, for bothFileInputStreamandChannelInputStreamProgress
Reviewing
Using
gitCheckout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/30357/head:pull/30357$ git checkout pull/30357Update a local copy of the PR:
$ git checkout pull/30357$ git pull https://git.openjdk.org/jdk.git pull/30357/headUsing Skara CLI tools
Checkout this PR locally:
$ git pr checkout 30357View PR using the GUI difftool:
$ git pr show -t 30357Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/30357.diff