RFR: 8344168: Change Unsafe base offset from int to long

Shaojin Wen swen at openjdk.org
Thu Nov 14 08:55:02 UTC 2024


On Thu, 14 Nov 2024 08:08:08 GMT, David Holmes <dholmes at openjdk.org> wrote:

>> The type of the Unsafe base offset constant is int, which may cause overflow when adding int offsets, such as 8343925 (PR #22012). 8343984 (PR #22027) fixes most of the offset overflows in JDK, but ArraysSupport and CRC32C are still unfixed.
>> 
>> @liach proposed the idea of ​​changing the Unsafe base offset to long, which is a complete solution to the Unsafe offset overflow. After discussing with @liach, I submitted this PR to implement @liach's idea.
>
> src/java.base/share/classes/java/util/zip/CRC32C.java line 227:
> 
>> 225:             long alignLength
>> 226:                     = (8 - ((Unsafe.ARRAY_BYTE_BASE_OFFSET + off) & 0x7)) & 0x7;
>> 227:             for (long alignEnd = off + alignLength; off < alignEnd; off++) {
> 
> I think casting the (now) long expression back to `int` makes more sense here.

alignLength is a very small value, which is an int, but alignEnd needs to be a long. Here, changing alignLength to a long can avoid alignEnd overflow.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/22095#discussion_r1841800097


More information about the core-libs-dev mailing list