RFR: 8286386: Address possibly lossy conversions in java.net.http [v5]

Daniel Fuchs dfuchs at openjdk.java.net
Thu May 12 09:03:53 UTC 2022


On Thu, 12 May 2022 08:42:39 GMT, Daniel Fuchs <dfuchs at openjdk.org> wrote:

>> codeLengthOf() returns long. It could be changed to return int with a cast internally and then you could avoid the two new casts.
>
> No because the int returned could be negative, while the long will not. Assuming bufferLen is 0 and codeLengthOf() returns some value that has the 32th bit set to 1 then when codeLengthOf() returns long, bufferLen + codeLengthOf() will be a positive long > 64, and we won't enter the `if` here but if codeLengthOf() returns `int`, then bufferLen + codeLengthOf() would be negative and the `if` would be wrongly entered. I am not 100% sure this is a scenario that might occur (codeLengthOf() returning large "unsigned int" values) - but I'd prefer to stay on the safe side and assume that it can.

This is what I mean:

jshell> long codeLengthOf = (long)Integer.MAX_VALUE + 1
codeLengthOf ==> 2147483648

jshell> int bufferLen = 0
bufferLen ==> 0

jshell> bufferLen + codeLengthOf <= 64
$3 ==> false

jshell> bufferLen + (int)codeLengthOf <= 64
$4 ==> true

-------------

PR: https://git.openjdk.java.net/jdk/pull/8656


More information about the net-dev mailing list