On Thu, 4 Sep 2025 15:22:57 GMT, Archie Cobbs <[email protected]> wrote:
> When bit shifting an `int` or `long` value by an amount `X`, all but the last
> 5 or 6 (respectively) bits of `X` are ignored.
>
> This can create a trap for the unwary, as in this example:
>
> public long readLongBigEndian(byte[] buf, int offset) {
> return ((buf[offset + 0] & 0xff) << 56) // BUG HERE
> | ((buf[offset + 1] & 0xff) << 48) // BUG HERE
> | ((buf[offset + 2] & 0xff) << 40) // BUG HERE
> | ((buf[offset + 3] & 0xff) << 32) // BUG HERE
> | ((buf[offset + 4] & 0xff) << 24)
> | ((buf[offset + 5] & 0xff) << 16)
> | ((buf[offset + 6] & 0xff) << 8)
> | ((buf[offset + 7] & 0xff);
> }
>
> This PR adds a new warning when the compiler detects an out-of-range bit
> shift, i.e., an `int` bit shift not in the range `[-31...31]` or a `long` bit
> shift not in the range `[-63...63]`.
This pull request has now been integrated.
Changeset: bc928c81
Author: Archie Cobbs <[email protected]>
URL:
https://git.openjdk.org/jdk/commit/bc928c814b5ea70505e362a643e18664e119bce3
Stats: 187 lines in 10 files changed: 183 ins; 0 del; 4 mod
5038439: Warning message for literal shift amounts outside the canonical domain
Reviewed-by: darcy, jlahoda
-------------
PR: https://git.openjdk.org/jdk/pull/27102