mirror of
https://github.com/monero-project/monero.git
synced 2025-01-19 16:13:40 +02:00
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.
This commit is contained in:
parent
2fe0f04c1e
commit
27858049da
@ -33,6 +33,7 @@
|
|||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
#include <sodium/crypto_verify_32.h>
|
#include <sodium/crypto_verify_32.h>
|
||||||
|
|
||||||
#define CRYPTO_MAKE_COMPARABLE(type) \
|
#define CRYPTO_MAKE_COMPARABLE(type) \
|
||||||
@ -60,14 +61,18 @@ namespace crypto { \
|
|||||||
namespace crypto { \
|
namespace crypto { \
|
||||||
static_assert(sizeof(std::size_t) <= sizeof(type), "Size of " #type " must be at least that of size_t"); \
|
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) { \
|
inline std::size_t hash_value(const type &_v) { \
|
||||||
return reinterpret_cast<const std::size_t &>(_v); \
|
std::size_t h; \
|
||||||
|
memcpy(&h, std::addressof(_v), sizeof(h)); \
|
||||||
|
return h; \
|
||||||
} \
|
} \
|
||||||
} \
|
} \
|
||||||
namespace std { \
|
namespace std { \
|
||||||
template<> \
|
template<> \
|
||||||
struct hash<crypto::type> { \
|
struct hash<crypto::type> { \
|
||||||
std::size_t operator()(const crypto::type &_v) const { \
|
std::size_t operator()(const crypto::type &_v) const { \
|
||||||
return reinterpret_cast<const std::size_t &>(_v); \
|
std::size_t h; \
|
||||||
|
memcpy(&h, std::addressof(_v), sizeof(h)); \
|
||||||
|
return h; \
|
||||||
} \
|
} \
|
||||||
}; \
|
}; \
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user