From d68c843b27375fad6778ea63a2700877924b4245 Mon Sep 17 00:00:00 2001 From: Alex Richardson Date: Sun, 15 May 2022 11:12:41 +0100 Subject: [PATCH] dbus-string.c: use memcpy() in _dbus_string_insert_{2,4,8}_aligned This fixes a -Wcast-align warning from Clang, and I believe this could be a genuine issue on some architectures since the octets argument is an unsigned char pointer that only has alignment 1. Using memcpy() instead will generate the same code on architectures that support unaligned loads and stores (i.e. almost all current ones) and individual stores on those that don't (e.g. old MIPS). --- dbus/dbus-string.c | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/dbus/dbus-string.c b/dbus/dbus-string.c index 6ea1c0395..05c832311 100644 --- a/dbus/dbus-string.c +++ b/dbus/dbus-string.c @@ -992,18 +992,6 @@ _dbus_string_append (DBusString *str, return append (real, buffer, buffer_len); } -/** assign 2 bytes from one string to another */ -#define ASSIGN_2_OCTETS(p, octets) \ - *((dbus_uint16_t*)(p)) = *((dbus_uint16_t*)(octets)); - -/** assign 4 bytes from one string to another */ -#define ASSIGN_4_OCTETS(p, octets) \ - *((dbus_uint32_t*)(p)) = *((dbus_uint32_t*)(octets)); - -/** assign 8 bytes from one string to another */ -#define ASSIGN_8_OCTETS(p, octets) \ - *((dbus_uint64_t*)(p)) = *((dbus_uint64_t*)(octets)); - /** * Inserts 2 bytes aligned on a 2 byte boundary * with any alignment padding initialized to 0. @@ -1023,7 +1011,7 @@ _dbus_string_insert_2_aligned (DBusString *str, if (!align_insert_point_then_open_gap (str, &insert_at, 2, 2)) return FALSE; - ASSIGN_2_OCTETS (real->str + insert_at, octets); + memcpy (real->str + insert_at, octets, 2); return TRUE; } @@ -1047,7 +1035,7 @@ _dbus_string_insert_4_aligned (DBusString *str, if (!align_insert_point_then_open_gap (str, &insert_at, 4, 4)) return FALSE; - ASSIGN_4_OCTETS (real->str + insert_at, octets); + memcpy (real->str + insert_at, octets, 4); return TRUE; } @@ -1072,8 +1060,8 @@ _dbus_string_insert_8_aligned (DBusString *str, return FALSE; _dbus_assert (_DBUS_ALIGN_VALUE (insert_at, 8) == (unsigned) insert_at); - - ASSIGN_8_OCTETS (real->str + insert_at, octets); + + memcpy (real->str + insert_at, octets, 8); return TRUE; } -- 2.47.3