parsingdata/metal

hashCode() implementation for classes with byte arrays is broken

Closed this issue · 0 comments

jvdb commented

Classes with a byte[] field (ConstantSource and InMemoryByteStream) had the following hashCode() implementation:

return Arrays.hashCode(field);

Where field was a byte[] field. This was changed by #195 (to address #150) to:

return Objects.hash(getClass().hashCode(), field);

However, this delegates to Arrays.hashCode(...), but to another overridden implementation of it. As a result, the byte[] is not deeply hashed, which will break the hashCode() contract.

For example, two instances of con(1) will be equal (because the equals() overrides do a deep comparison), but the hashCode() will produce distinct values because both instances will have a unique byte[] array and their hash code is based on their distinct reference.

The hashCode() implementation should be changed to:

return Objects.hash(getClass().hashCode(), Arrays.hashCode(field));