From b03ecb12e51ee38973a63f35fd3163971dca8f1a Mon Sep 17 00:00:00 2001 From: Caleb Sander Date: Thu, 2 Nov 2023 14:26:36 +0100 Subject: [PATCH] test: Avoid unaligned pointer dereferences Avoid casting byte-aligned pointers to pointers with higher alignment. Loading or storing values with higher alignment is undefined behavior, since some processors don't allow unaligned memory accesses and compilers may assume pointers of different types don't alias. Perform an explicit memcpy(), which an optimizing compiler can easily replace with a single load/store on supported architectures. Signed-off-by: Caleb Sander --- test/mi-mctp.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/mi-mctp.c b/test/mi-mctp.c index 1ba2ae8a..5711c030 100644 --- a/test/mi-mctp.c +++ b/test/mi-mctp.c @@ -83,12 +83,14 @@ static void test_set_tx_mic(struct test_peer *peer) { extern __u32 nvme_mi_crc32_update(__u32 crc, void *data, size_t len); __u32 crc = 0xffffffff; + __le32 crc_le; - assert(peer->tx_buf_len + sizeof(crc) <= MAX_BUFSIZ); + assert(peer->tx_buf_len + sizeof(crc_le) <= MAX_BUFSIZ); crc = nvme_mi_crc32_update(crc, peer->tx_buf, peer->tx_buf_len); - *(uint32_t *)(peer->tx_buf + peer->tx_buf_len) = cpu_to_le32(~crc); - peer->tx_buf_len += sizeof(crc); + crc_le = cpu_to_le32(~crc); + memcpy(peer->tx_buf + peer->tx_buf_len, &crc_le, sizeof(crc_le)); + peer->tx_buf_len += sizeof(crc_le); } int __wrap_socket(int family, int type, int protocol) -- 2.50.1