Skip to content

Commit 2785804

Browse files
committed
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.
1 parent 2fe0f04 commit 2785804

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/crypto/generic-ops.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <cstddef>
3434
#include <cstring>
3535
#include <functional>
36+
#include <memory>
3637
#include <sodium/crypto_verify_32.h>
3738

3839
#define CRYPTO_MAKE_COMPARABLE(type) \
@@ -60,14 +61,18 @@ namespace crypto { \
6061
namespace crypto { \
6162
static_assert(sizeof(std::size_t) <= sizeof(type), "Size of " #type " must be at least that of size_t"); \
6263
inline std::size_t hash_value(const type &_v) { \
63-
return reinterpret_cast<const std::size_t &>(_v); \
64+
std::size_t h; \
65+
memcpy(&h, std::addressof(_v), sizeof(h)); \
66+
return h; \
6467
} \
6568
} \
6669
namespace std { \
6770
template<> \
6871
struct hash<crypto::type> { \
6972
std::size_t operator()(const crypto::type &_v) const { \
70-
return reinterpret_cast<const std::size_t &>(_v); \
73+
std::size_t h; \
74+
memcpy(&h, std::addressof(_v), sizeof(h)); \
75+
return h; \
7176
} \
7277
}; \
7378
}

0 commit comments

Comments
 (0)