fury icon indicating copy to clipboard operation
fury copied to clipboard

[Java] MemoryBuffer::readBytesAsInt64 with len=0 Causes '>>>' Shift Operation by Overly Large Constant Value Issue

Open LouisLou2 opened this issue 9 months ago • 0 comments

Search before asking

  • [x] I had searched in the issues and found no similar issues.

Version

Fury Java 0.10.0

Component(s)

Java

Minimal reproduce step

  1. Use the following method in your Java code:

    /** Read {@code len} bytes into a long using little-endian order. */
    public long readBytesAsInt64(int len) {
        int readerIdx = readerIndex;
        int remaining = size - readerIdx;
        if (remaining >= 8) {
            readerIndex = readerIdx + len;
            long v = UNSAFE.getLong(heapMemory, address + readerIdx);
            v = (LITTLE_ENDIAN ? v : Long.reverseBytes(v)) & (0xffffffffffffffffL >>> ((8 - len) * 8));
            return v;
        }
        return slowReadBytesAsInt64(remaining, len);
    }
    
  2. Call this method with len = 0.

What did you expect to see?

When len = 0, I expected the bitwise AND operation (&) with 0xffffffffffffffffL >>> 64 to return a value of 0.

What did you see instead?

In Java 23, the following error is reported:
Shift operation '>>>' by overly large constant value 64

In earlier versions of Java (e.g., 1.8), there is no error, but the result does not match expectations. The operation 0xffffffffffffffffL >>> 64 essentially leaves the result unchanged, where the long value becomes populated with unrelated values, leading to unpredictable outcomes.

In Dart, the same mask (0xffffffffffffffffL >>> 64) would result in 0 as expected.

Anything Else?

This issue could lead to unexpected behavior in different Java versions, and it may require adjustments to the code to handle len = 0 safely across all environments. Please consider revising the implementation to handle the shift operation correctly for len = 0.

Are you willing to submit a PR?

  • [x] I'm willing to submit a PR!

LouisLou2 avatar Mar 11 '25 16:03 LouisLou2