From 6327adafe20603fce3c7507e20f300e07398b517 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Tue, 21 Jun 2011 17:02:10 +0100 Subject: [PATCH] dbus-marshal-basic: assert that the size and (on gcc) alignment of all types is as expected The __alignof__() checks here will fail if compiling for an architecture where more-than-natural alignment is needed. Bug: https://bugs.freedesktop.org/show_bug.cgi?id=39636 Reviewed-by: Will Thompson --- dbus/dbus-marshal-basic.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/dbus/dbus-marshal-basic.c b/dbus/dbus-marshal-basic.c index 3cbc721..215cc95 100644 --- a/dbus/dbus-marshal-basic.c +++ b/dbus/dbus-marshal-basic.c @@ -29,6 +29,39 @@ #include +#if defined(__GNUC__) && (__GNUC__ >= 4) +# define _DBUS_ASSERT_ALIGNMENT(type, op, val) \ + _DBUS_STATIC_ASSERT (__extension__ __alignof__ (type) op val) +#else +# define _DBUS_ASSERT_ALIGNMENT(type, op, val) do { } while (0) +#endif + +/* True by definition, but just for completeness... */ +_DBUS_STATIC_ASSERT (sizeof (char) == 1); +_DBUS_ASSERT_ALIGNMENT (char, ==, 1); + +_DBUS_STATIC_ASSERT (sizeof (dbus_int16_t) == 2); +_DBUS_ASSERT_ALIGNMENT (dbus_int16_t, <=, 2); +_DBUS_STATIC_ASSERT (sizeof (dbus_uint16_t) == 2); +_DBUS_ASSERT_ALIGNMENT (dbus_uint16_t, <=, 2); + +_DBUS_STATIC_ASSERT (sizeof (dbus_int32_t) == 4); +_DBUS_ASSERT_ALIGNMENT (dbus_int32_t, <=, 4); +_DBUS_STATIC_ASSERT (sizeof (dbus_uint32_t) == 4); +_DBUS_ASSERT_ALIGNMENT (dbus_uint32_t, <=, 4); +_DBUS_STATIC_ASSERT (sizeof (dbus_bool_t) == 4); +_DBUS_ASSERT_ALIGNMENT (dbus_bool_t, <=, 4); + +_DBUS_STATIC_ASSERT (sizeof (double) == 8); +_DBUS_ASSERT_ALIGNMENT (double, <=, 8); + +#ifdef DBUS_HAVE_INT64 +_DBUS_STATIC_ASSERT (sizeof (dbus_int64_t) == 8); +_DBUS_ASSERT_ALIGNMENT (dbus_int64_t, <=, 8); +_DBUS_STATIC_ASSERT (sizeof (dbus_uint64_t) == 8); +_DBUS_ASSERT_ALIGNMENT (dbus_uint64_t, <=, 8); +#endif + /** * @defgroup DBusMarshal marshaling and unmarshaling * @ingroup DBusInternals -- 2.7.4