commit 5df018ff07ef1aef0762d98cceb95fa04dbdb706 from: Stefan Sperling date: Thu Oct 14 16:03:22 2021 UTC rework murmurhash2() to avoid potential unaligned memory access pointed out by naddy@ ok millert@ commit - 1d19226a8a09c02a94d6ccee03f964fd413f2fe1 commit + 5df018ff07ef1aef0762d98cceb95fa04dbdb706 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);