Skip to content

ghash: Correctly retrieve low 32 bits of 64-bit values

Simon McVittie requested to merge wip/smcv/64-bit-hash into main

Dereferencing a pointer to a 64-bit object as though it was a 32-bit object is the same as taking the least significant 32 bits on a little-endian machine, but on a big-endian machine it is the same as taking the most significant 32 bits, which would result in these hash functions always hashing to zero. The /hash/int64/collisions and /hash/double/collisions test-cases in glib/tests/hash.c detect this and fail.

Instead, fetch the whole 64-bit quantity and do the bit-manipulation to xor the more significant half with the less significant half in an architecture-neutral way.

Fixes: dd1f4f70 "Optimize g_double_hash implementation"
Fixes: c1af4b2b "Optional optimization for g_int64_hash"
Resolves: #2787 (closed)


/cc @3v1n0 @pwithnall @wszqkzqk

We should either cherry-pick this to 2.74.x before 2.74.1, or revert !2924 (merged) in the 2.74.x branch. I would personally revert.

Merge request reports

Loading