https://bz.apache.org/bugzilla/show_bug.cgi?id=70321

abernal <[email protected]> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 OS|                            |All

--- Comment #1 from abernal <[email protected]> ---
Reproduced on a running Tomcat built from main, using the loop from the
description with a 23 byte POST body:

- 6fd26942a4 (parent of e9ff675a): the loop reads 23 bytes, the next read(data,
23, 0) returns -1 and the loop ends after 1 iteration.
- adc24f0623 (current main): after the 23 bytes, read(data, 23, 0) returns 0
every time; 99.5 million iterations in 2 seconds before my test guard stopped
it.

The new behaviour is what the InputStream contract requires. The Javadoc of
InputStream.read(byte[], int, int) says: "If len is zero, then no bytes are
read and 0 is returned; otherwise, there is an attempt to read at least one
byte. If no byte is available because the stream is at end of file, the value
-1 is returned". The len == 0 case comes first, so 0 is the correct result at
end of stream too. The previous -1 was the non-compliant behaviour.

The loop is not portable: in the same test BufferedInputStream.read(b, 0, 0) at
end of stream returns 0, so it would spin there as well (ByteArrayInputStream
returns -1, so the JDK itself is not consistent). The loop needs to stop when
bytesLeft reaches 0, e.g. while (bytesLeft > 0 && (n = in.read(data, totalRead,
bytesLeft)) != -1).

The change is visible to applications that relied on the old result, so a note
in the changelog may help, but I don't think the fix should be reverted.

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to