LouisLou2 opened a new issue, #2110:
URL: https://github.com/apache/fury/issues/2110

   ### Search before asking
   
   - [x] I had searched in the [issues](https://github.com/apache/fury/issues) 
and found no similar issues.
   
   
   ### Version
   
   0.10.0
   
   ### Component(s)
   
   Java
   
   ### Minimal reproduce step
   
   ```java
   public static void main(String[] args) {
     // With buffer size 9 - outputs 68719476735
     MemoryBuffer buffer1 = MemoryBuffer.newHeapBuffer(9);
     int off1 = buffer1._unsafePutVarUint36Small(0, 68719476735L);
     long res1 = buffer1.readVarUint36Small();
     System.out.println(res1);  // Outputs: 68719476735
     
     // With buffer size 8 - outputs 34359738367 (incorrect)
     MemoryBuffer buffer2 = MemoryBuffer.newHeapBuffer(8);
     int off2 = buffer2._unsafePutVarUint36Small(0, 68719476735L);
     long res2 = buffer2.readVarUint36Small();
     System.out.println(res2);  // Outputs: 34359738367
   }
   ```
   
   ### What did you expect to see?
   
   The same value (68719476735) should be read regardless of the initial buffer 
size. The reading behavior should be consistent and should not depend on the 
remaining buffer size.
   
   ### What did you see instead?
   
   Different results depending on buffer size:
   
   With buffer size 9: correct output 68719476735
   With buffer size 8: incorrect output 34359738367
   
   ### Anything Else?
   
   I found that `readVarUint36Small()` has two different code paths depending 
on the remaining buffer size:
   
   1. When `size - readIdx >= 9`, it uses a "fast" path that can read up to 36 
bits of data
   2. When `size - readIdx < 9`, it falls back to `readVarUint36Slow()` which 
only reads up to 35 bits (5 groups of 7 bits)
   
   In `readVarUint36Small()` fast path, the last bit extraction does:
   ```java
   // 0xff0000000: 0b11111111 << 28
   result |= (bulkValue >>> 4) & 0xff0000000L;
   ```
   This extracts 8 bits for the high order bits.
   
   Meanwhile, the slow path in `readVarUint36Slow()` adds 7 bits up to 5 times, 
reaching a maximum of 35 bits.
   
   This inconsistency causes different values to be read depending only on the 
buffer size, not on the actual data. There needs to be alignment between the 
"fast" and "slow" paths - either both should read 35 bits or both should read 
36 bits.
   
   ### Are you willing to submit a PR?
   
   - [x] I'm willing to submit a PR!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to