]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
dbus-string.c: use memcpy() in _dbus_string_insert_{2,4,8}_aligned
authorAlex Richardson <arichardson.kde@gmail.com>
Sun, 15 May 2022 10:12:41 +0000 (11:12 +0100)
committerAlexander Richardson <alexander.richardson@cl.cam.ac.uk>
Tue, 17 May 2022 11:21:33 +0000 (11:21 +0000)
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

index 6ea1c039566c1822abf8dc40d77df57a58d91aad..05c832311185d788ad12ce51d8e7d32e3b7d3a3e 100644 (file)
@@ -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;
 }