You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/** Read {@code len} bytes into a long using little-endian order. */publiclongreadBytesAsInt64(intlen) {
intreaderIdx = readerIndex;
// use subtract to avoid overflowintremaining = size - readerIdx;
if (remaining >= 8) {
readerIndex = readerIdx + len;
longv =
UNSAFE.getLong(heapMemory, address + readerIdx)
& (0xffffffffffffffffL >>> ((8 - len) * 8));
returnLITTLE_ENDIAN ? v : Long.reverseBytes(v);
}
returnslowReadBytesAsInt64(remaining, len);
}
I have some doubts about this method above when applying big endian.
Problem Analysis
Incorrect Mask Calculation
The issue arises when len < 8. The current mask calculation:
0xffffffffffffffffL >>> ((8 - len) * 8)
keeps the lower len * 8 bits, but in the Big Endian scenario, the valid data may actually reside in the higher len * 8 bits (the upper bits).
Example:
When len = 3, the mask calculation becomes:
0xffffffffffffffffL >>> 40
which results in the mask 0x0000000000ffffff, effectively keeping the lower 24 bits. However, if the data is stored in Big Endian, the valid data would actually reside in the upper 24 bits (the first 3 bytes). As a result, this incorrect mask clears the valid data.
Incorrect Byte Order Handling
The byte order reversal operation should happen before applying the mask. Currently, the reversal happens after the mask is applied:
returnLITTLE_ENDIAN ? v : Long.reverseBytes(v);
This is problematic because, after the byte order is reversed, the high-order bits may get erroneously cleared due to the mask being applied in the wrong order. Reversing the byte order after masking results in the potential loss of the valid high-order bits.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have some doubts about this method above when applying big endian.
Problem Analysis
Incorrect Mask Calculation
len < 8
. The current mask calculation:len * 8
bits, but in the Big Endian scenario, the valid data may actually reside in the higherlen * 8
bits (the upper bits).Example:
When
len = 3
, the mask calculation becomes:which results in the mask
0x0000000000ffffff
, effectively keeping the lower 24 bits. However, if the data is stored in Big Endian, the valid data would actually reside in the upper 24 bits (the first 3 bytes). As a result, this incorrect mask clears the valid data.Incorrect Byte Order Handling
Beta Was this translation helpful? Give feedback.
All reactions