From 3ca3c94dcc29e97f0271125b7ae5f9dc3afaac2d Mon Sep 17 00:00:00 2001 From: Stefan Sperling Date: Thu, 14 Oct 2021 16:03:22 +0000 Subject: [PATCH] rework murmurhash2() to avoid potential unaligned memory access pointed out by naddy@ ok millert@ --- lib/murmurhash2.c | 11 +++++++---- lib/murmurhash2.h | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/murmurhash2.c b/lib/murmurhash2.c index ac869f86..d3a67547 100644 --- a/lib/murmurhash2.c +++ b/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,11 +24,13 @@ 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; @@ -58,4 +61,4 @@ murmurhash2(const void * key, int len, uint32_t seed) h ^= h >> 15; return h; -} +} diff --git a/lib/murmurhash2.h b/lib/murmurhash2.h index bbd2bf3e..8fa42cdb 100644 --- a/lib/murmurhash2.h +++ b/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); -- 2.11.4.GIT