]> www.infradead.org Git - users/sagi/libnvme.git/commitdiff
test: Avoid unaligned pointer dereferences
authorCaleb Sander <csander@purestorage.com>
Thu, 2 Nov 2023 13:26:36 +0000 (14:26 +0100)
committerDaniel Wagner <wagi@monom.org>
Thu, 2 Nov 2023 13:31:26 +0000 (14:31 +0100)
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 <csander@purestorage.com>
test/mi-mctp.c

index 1ba2ae8a7a65647d0ace8d6de9c08f31b4757108..5711c0305bbb960a868512936400273f6ce96097 100644 (file)
@@ -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)