commit 3ca3c94dcc29e97f0271125b7ae5f9dc3afaac2d from: Stefan Sperling via: Thomas Adam date: Fri Oct 15 19:22:03 2021 UTC rework murmurhash2() to avoid potential unaligned memory access pointed out by naddy@ ok millert@ commit - b7e0a38461dd7c0fb1261f45640ec533e7305709 commit + 3ca3c94dcc29e97f0271125b7ae5f9dc3afaac2d blob - ac869f866068f2b1050c52779f2539890a8c877f blob + d3a67547fa3ddbd12660f688766bd387d6ed905b --- lib/murmurhash2.c +++ lib/murmurhash2.c @@ -5,11 +5,12 @@ /* Obtained from https://github.com/aappleby/smhasher */ #include +#include #include "murmurhash2.h" uint32_t -murmurhash2(const void * key, int len, uint32_t seed) +murmurhash2(const unsigned char * key, int len, uint32_t seed) { // 'm' and 'r' are mixing constants generated offline. // They're not really 'magic', they just happen to work well. @@ -23,12 +24,14 @@ murmurhash2(const void * key, int len, uint32_t seed) // Mix 4 bytes at a time into the hash - const unsigned char *data = (const unsigned char *)key; + const unsigned char *data = key; while(len >= 4) { - uint32_t k = *(uint32_t*)data; + uint32_t k; + memcpy(&k, data, sizeof(k)); + k *= m; k ^= k >> r; k *= m; @@ -58,4 +61,4 @@ murmurhash2(const void * key, int len, uint32_t seed) h ^= h >> 15; return h; -} +} blob - bbd2bf3ef0309efebdfbb79dd75aa100d4fb0b74 blob + 8fa42cdb7d310218bbaff43e40157c42561bdebd --- lib/murmurhash2.h +++ lib/murmurhash2.h @@ -4,4 +4,4 @@ /* Obtained from https://github.com/aappleby/smhasher */ -uint32_t murmurhash2(const void *key, int len, uint32_t seed); +uint32_t murmurhash2(const unsigned char *key, int len, uint32_t seed);