From: Otto Moerbeek Date: Tue, 22 Feb 2022 13:26:26 +0000 (+0100) Subject: rec: fix unaligned access is murmur hash code used by NOD X-Git-Tag: rec-4.7.0-alpha1~7^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9be46e86ce3c394b601bfce7c387f63ec87e02a9;p=thirdparty%2Fpdns.git rec: fix unaligned access is murmur hash code used by NOD --- diff --git a/pdns/recursordist/stable-bloom.hh b/pdns/recursordist/stable-bloom.hh index 3fc14cce89..877ed19f36 100644 --- a/pdns/recursordist/stable-bloom.hh +++ b/pdns/recursordist/stable-bloom.hh @@ -28,6 +28,7 @@ #include #include #include "misc.hh" +#include "noinitvector.hh" #include "ext/probds/murmur3.h" namespace bf @@ -164,8 +165,18 @@ private: std::vector hash(const std::string& data) const { uint32_t h1, h2; - MurmurHash3_x86_32(data.c_str(), data.length(), 1, (void*)&h1); - MurmurHash3_x86_32(data.c_str(), data.length(), 2, (void*)&h2); + // MurmurHash3 assumes the data is uint32_t aligned, so fixup if needed + // It does handle string lengths that are not a multiple of sizeof(uint32_t) correctly + if (reinterpret_cast(data.data()) % sizeof(uint32_t) != 0) { + NoInitVector x((data.length() / sizeof(int32_t)) + 1); + memcpy(x.data(), data.data(), data.length()); + MurmurHash3_x86_32(x.data(), data.length(), 1, &h1); + MurmurHash3_x86_32(x.data(), data.length(), 2, &h2); + } + else { + MurmurHash3_x86_32(data.data(), data.length(), 1, &h1); + MurmurHash3_x86_32(data.data(), data.length(), 2, &h2); + } std::vector ret_hashes(d_k); for (size_t i = 0; i < d_k; ++i) { ret_hashes[i] = h1 + i * h2;