JDK-8321429 breaks FileKey.hashCode()

Simon Nash simon at cjnash.com
Sun Jan 14 23:54:47 UTC 2024


The change for JDK-8321429 breaks the hashCode() method of sun.nio.ch.FileKey.

Before this change, the hashCode was computed from long values as follows:

return (int)(dwVolumeSerialNumber ^ (dwVolumeSerialNumber >>> 32)) +
        (int)(nFileIndexHigh ^ (nFileIndexHigh >>> 32)) +
        (int)(nFileIndexLow ^ (nFileIndexLow >>> 32));

This adds the three 32-bit values for volumeSerialNumber, fileIndexHigh and fileIndexLow, producing a reasonable hash.

With the change, the hashCode is computed from int values as follows:

int h = dwVolumeSerialNumber;
h = h << 31 + nFileIndexHigh;
h = h << 31 + nFileIndexLow;
return h;

Because << has lower precedence than +, this shifts the serial number out of h twice and is very likely to return zero.
My test results:  serialNumber=0xc8b4b431 indexHigh=0x00240000 indexLow=0x00023be9 h=0x00000000

This should be reverted to the previous algorithm suitably adjusted to use the new int values:

return dwVolumeSerialNumber +
        nFileIndexHigh +
        nFileIndexLow;

Simon




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/nio-dev/attachments/20240114/410eddda/attachment.htm>


More information about the nio-dev mailing list