From 74e1001646d2906cec717f4b48862ba6212927b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rinc?= Date: Wed, 28 Jan 2026 14:13:38 +0100 Subject: [PATCH 1/3] test: avoid strict-aliasing/alignment UB in unit tests The unit tests were writing through typed pointers into byte storage: - `cuckoocache_tests.cpp` casted `uint256::begin()` to `uint32_t*` and wrote words. This is strict-aliasing UB (and can be miscompiled under -O2). Replace with `WriteLE32()` stores into the byte buffer. - `allocator_tests.cpp` casted an untyped `void*` allocation to `uint32_t*` and dereferenced it. This can be misaligned access UB on some platforms. Replace with `std::memcpy()` store/load. --- src/test/allocator_tests.cpp | 9 +++++++-- src/test/cuckoocache_tests.cpp | 29 +++++++++++++++++------------ 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/test/allocator_tests.cpp b/src/test/allocator_tests.cpp index 65a0629d9e82..f454bb49d887 100644 --- a/src/test/allocator_tests.cpp +++ b/src/test/allocator_tests.cpp @@ -5,6 +5,8 @@ #include #include +#include +#include #include #include #include @@ -220,8 +222,11 @@ BOOST_AUTO_TEST_CASE(lockedpool_tests_live) void *a0 = pool.alloc(16); BOOST_CHECK(a0); // Test reading and writing the allocated memory - *((uint32_t*)a0) = 0x1234; - BOOST_CHECK(*((uint32_t*)a0) == 0x1234); + uint32_t value = 0x1234; + std::memcpy(a0, &value, sizeof(value)); + uint32_t read_value; + std::memcpy(&read_value, a0, sizeof(read_value)); + BOOST_CHECK_EQUAL(read_value, value); pool.free(a0); try { // Test exception on double-free diff --git a/src/test/cuckoocache_tests.cpp b/src/test/cuckoocache_tests.cpp index 5c723596e77a..43418acb57b9 100644 --- a/src/test/cuckoocache_tests.cpp +++ b/src/test/cuckoocache_tests.cpp @@ -3,6 +3,7 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include +#include #include #include