From 27858049da753ea7d0ce44829a6c06f1597d72d5 Mon Sep 17 00:00:00 2001 From: jeffro256 Date: Thu, 9 Jan 2025 00:48:51 -0600 Subject: [PATCH] crypto: make CRYPTO_DEFINE_HASH_FUNCTIONS adhere strict aliasing This code could've caused issues if the pointer to the `public_key`, `key_image`, `hash`, etc wasn't aligned on an 8-byte boundary. --- src/crypto/generic-ops.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/crypto/generic-ops.h b/src/crypto/generic-ops.h index 5a5e09f9b62..3ff3619fee8 100644 --- a/src/crypto/generic-ops.h +++ b/src/crypto/generic-ops.h @@ -33,6 +33,7 @@ #include #include #include +#include #include #define CRYPTO_MAKE_COMPARABLE(type) \ @@ -60,14 +61,18 @@ namespace crypto { \ namespace crypto { \ static_assert(sizeof(std::size_t) <= sizeof(type), "Size of " #type " must be at least that of size_t"); \ inline std::size_t hash_value(const type &_v) { \ - return reinterpret_cast(_v); \ + std::size_t h; \ + memcpy(&h, std::addressof(_v), sizeof(h)); \ + return h; \ } \ } \ namespace std { \ template<> \ struct hash { \ std::size_t operator()(const crypto::type &_v) const { \ - return reinterpret_cast(_v); \ + std::size_t h; \ + memcpy(&h, std::addressof(_v), sizeof(h)); \ + return h; \ } \ }; \ }