]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
Define _DBUS_ALIGNOF using _Alignof when using C11 or newer
authorKhem Raj <raj.khem@gmail.com>
Sat, 14 Jan 2023 20:39:43 +0000 (12:39 -0800)
committerSimon McVittie <smcv@collabora.com>
Wed, 8 Feb 2023 10:03:39 +0000 (10:03 +0000)
WG14 N2350 made very clear that it is an UB having type definitions
within "offsetof" [1]. This patch changes the implementation of macro
_DBUS_ALIGNOF to builtin "_Alignof" to avoid undefined behavior.

clang 16+ has started to diagnose this [2]

Fixes build when using -std >= gnu11 and using clang16+

[1] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2350.htm
[2] https://reviews.llvm.org/D133574

Signed-off-by: Khem Raj <raj.khem@gmail.com>
(cherry picked from commit c3e2c873ffa8e89b5f83dccc4e4e35dfcea633cb)

dbus/dbus-internals.h

index 3b293b057705d422dfc83068661bb5f0e984d150..739b1b265caeb15697421805ca56060d7bf4390e 100644 (file)
@@ -201,8 +201,12 @@ void _dbus_real_assert_not_reached (const char *explanation,
     ((intptr_t) ((unsigned char*) &((struct_type*) 0)->member))
 #endif
 
+#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__cplusplus)
+#define _DBUS_ALIGNOF(type) _Alignof(type)
+#else
 #define _DBUS_ALIGNOF(type) \
     (_DBUS_STRUCT_OFFSET (struct { char _1; type _2; }, _2))
+#endif
 
 #if defined(DBUS_DISABLE_CHECKS) || defined(DBUS_DISABLE_ASSERT)
 /* this is an assert and not an error, but in the typical --disable-checks case (you're trying