From: Jeremy Kerr Date: Mon, 26 Sep 2022 12:02:31 +0000 (+0800) Subject: util: Fix le128_to_cpu on big-endian X-Git-Tag: v2.2~24^2 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=2308389a47aeb2a573eca4eb63c494a3723d3c6a;p=users%2Fsagi%2Fnvme-cli.git util: Fix le128_to_cpu on big-endian We currently have a bug when converting from le128, as we'll overwrite words that we later need to read. Also, use an if rather than #ifdef, so the BE case can be compiled. Turns out we need the ccan headers for this. Signed-off-by: Jeremy Kerr --- diff --git a/tests/meson.build b/tests/meson.build index 1ce6463a..40632b2a 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -52,6 +52,7 @@ endif test_uint128 = executable( 'test-uint128', ['test-uint128.c', '../util/types.c'], + include_directories: [incdir, '..'], dependencies: [libuuid_dep], ) diff --git a/util/types.c b/util/types.c index d5de2f27..fd8113c4 100644 --- a/util/types.c +++ b/util/types.c @@ -4,20 +4,25 @@ #include #include +#include + #include "types.h" nvme_uint128_t le128_to_cpu(__u8 *data) { nvme_uint128_t u; - memcpy(u.bytes, data, 16); + if (HAVE_BIG_ENDIAN) { + nvme_uint128_t tmp; + memcpy(tmp.bytes, data, 16); + u.words[0] = le32_to_cpu(tmp.words[3]); + u.words[1] = le32_to_cpu(tmp.words[2]); + u.words[2] = le32_to_cpu(tmp.words[1]); + u.words[3] = le32_to_cpu(tmp.words[0]); + } else { + memcpy(u.bytes, data, 16); + } -#if HAVE_BIG_ENDIAN - u.words[0] = le32_to_cpu(u.words[3]); - u.words[1] = le32_to_cpu(u.words[2]); - u.words[2] = le32_to_cpu(u.words[1]); - u.words[3] = le32_to_cpu(u.words[0]); -#endif return u; }