From 5a189bdd416c5afb61b66871231e6dbde288a96a Mon Sep 17 00:00:00 2001 From: Ulrich Weigand Date: Thu, 12 Sep 2019 15:46:06 +0200 Subject: [PATCH] cmake: Fix unaligned check on big-endian systems On big-endian systems, ntohl is a no-op, so "good" never does any conversion. Fix this by keeping the test constants in big-endian (network) order and using ntohl to convert them to native (host) byte order. Signed-off-by: Ulrich Weigand --- cmake/modules/CephChecks.cmake | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/cmake/modules/CephChecks.cmake b/cmake/modules/CephChecks.cmake index f6dd41cf105..2bc850456ad 100644 --- a/cmake/modules/CephChecks.cmake +++ b/cmake/modules/CephChecks.cmake @@ -104,23 +104,18 @@ uint32_t load(char* p, size_t offset) return *reinterpret_cast(p + offset); } -bool good(uint32_t lhs, uint32_t small_endian) +bool good(uint32_t lhs, uint32_t big_endian) { - uint32_t rhs; - if (ntohl(0xdeadbeaf) == 0xdeadbeaf) { - return lhs == ntohl(small_endian); - } else { - return lhs == small_endian; - } + return lhs == ntohl(big_endian); } int main(int argc, char **argv) { char a1[] = \"ABCDEFG\"; - uint32_t a2[] = {0x44434241, - 0x45444342, - 0x46454443, - 0x47464544}; + uint32_t a2[] = {0x41424344, + 0x42434445, + 0x43444546, + 0x44454647}; for (size_t i = 0; i < std::size(a2); i++) { if (!good(load(a1, i), a2[i])) { return 1; -- 2.39.5