From 1558a1f491d3c76b81db195ca2a616ff6683fccb Mon Sep 17 00:00:00 2001 From: Jared Tate <13957390+JaredTate@users.noreply.github.com> Date: Tue, 21 Jan 2025 09:32:04 -0700 Subject: [PATCH 1/3] Fix Improper Memory Alignment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is undefined behavior when ptr is not guaranteed to be 4-byte-aligned (for a uint32_t), or 8-byte-aligned (for a uint64_t). Many compilers and platforms allow unaligned reads if you compile with certain flags, but the C++ standard does not guarantee it is safe—and AddressSanitizer flags it. --- src/crypto/common.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crypto/common.h b/src/crypto/common.h index b29e1aac5c..c249fc5907 100644 --- a/src/crypto/common.h +++ b/src/crypto/common.h @@ -93,4 +93,4 @@ uint64_t static inline CountBits(uint64_t x) return ret; } -#endif // DIGIBYTE_CRYPTO_COMMON_H +#endif // DIGIBYTE_CRYPTO_COMMON_H \ No newline at end of file From 2fb0901f54ec8424fba12d6982b1c564fed4a085 Mon Sep 17 00:00:00 2001 From: Jared Tate <13957390+JaredTate@users.noreply.github.com> Date: Tue, 21 Jan 2025 09:46:49 -0700 Subject: [PATCH 2/3] Fix Improper Memory Alignment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is undefined behavior when ptr is not guaranteed to be 4-byte-aligned (for a uint32_t), or 8-byte-aligned (for a uint64_t). Many compilers and platforms allow unaligned reads if you compile with certain flags, but the C++ standard does not guarantee it is safe—and AddressSanitizer flags it. --- src/crypto/common.h | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/src/crypto/common.h b/src/crypto/common.h index c249fc5907..f26bd8f45a 100644 --- a/src/crypto/common.h +++ b/src/crypto/common.h @@ -17,32 +17,41 @@ uint16_t static inline ReadLE16(const unsigned char* ptr) { - return le16toh(*((uint16_t*)ptr)); + uint16_t x; + memcpy((char*)&x, ptr, 2); + return le16toh(x); } uint32_t static inline ReadLE32(const unsigned char* ptr) { - return le32toh(*((uint32_t*)ptr)); + uint32_t x; + memcpy((char*)&x, ptr, 4); + return le32toh(x); } uint64_t static inline ReadLE64(const unsigned char* ptr) { - return le64toh(*((uint64_t*)ptr)); + uint64_t x; + memcpy((char*)&x, ptr, 8); + return le64toh(x); } void static inline WriteLE16(unsigned char* ptr, uint16_t x) { - *((uint16_t*)ptr) = htole16(x); + uint16_t v = htole16(x); + memcpy(ptr, (char*)&v, 2); } void static inline WriteLE32(unsigned char* ptr, uint32_t x) { - *((uint32_t*)ptr) = htole32(x); + uint32_t v = htole32(x); + memcpy(ptr, (char*)&v, 4); } void static inline WriteLE64(unsigned char* ptr, uint64_t x) { - *((uint64_t*)ptr) = htole64(x); + uint64_t v = htole64(x); + memcpy(ptr, (char*)&v, 8); } uint16_t static inline ReadBE16(const unsigned char* ptr) @@ -54,22 +63,28 @@ uint16_t static inline ReadBE16(const unsigned char* ptr) uint32_t static inline ReadBE32(const unsigned char* ptr) { - return be32toh(*((uint32_t*)ptr)); + uint32_t x; + memcpy((char*)&x, ptr, 4); + return be32toh(x); } uint64_t static inline ReadBE64(const unsigned char* ptr) { - return be64toh(*((uint64_t*)ptr)); + uint64_t x; + memcpy((char*)&x, ptr, 8); + return be64toh(x); } void static inline WriteBE32(unsigned char* ptr, uint32_t x) { - *((uint32_t*)ptr) = htobe32(x); + uint32_t v = htobe32(x); + memcpy(ptr, (char*)&v, 4); } void static inline WriteBE64(unsigned char* ptr, uint64_t x) { - *((uint64_t*)ptr) = htobe64(x); + uint64_t v = htobe64(x); + memcpy(ptr, (char*)&v, 8); } /** Return the smallest number n such that (x >> n) == 0 (or 64 if the highest bit in x is set. */ @@ -93,4 +108,4 @@ uint64_t static inline CountBits(uint64_t x) return ret; } -#endif // DIGIBYTE_CRYPTO_COMMON_H \ No newline at end of file +#endif // DIGIBYTE_CRYPTO_COMMON_ \ No newline at end of file From 385491ccd6ca7892b0c5a3e8efa9943ba3465edd Mon Sep 17 00:00:00 2001 From: Jared Tate <13957390+JaredTate@users.noreply.github.com> Date: Tue, 21 Jan 2025 10:10:02 -0700 Subject: [PATCH 3/3] Update common.h --- src/crypto/common.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crypto/common.h b/src/crypto/common.h index f26bd8f45a..99c46bff24 100644 --- a/src/crypto/common.h +++ b/src/crypto/common.h @@ -108,4 +108,4 @@ uint64_t static inline CountBits(uint64_t x) return ret; } -#endif // DIGIBYTE_CRYPTO_COMMON_ \ No newline at end of file +#endif // DIGIBYTE_CRYPTO_COMMON_H \ No newline at end of file