GVariant: deal with non-8-aligned malloc()
[platform/upstream/glib.git] / glib / gvariant-serialiser.c
index cf23812..a94a485 100644 (file)
@@ -139,6 +139,24 @@ g_variant_serialised_check (GVariantSerialised serialised)
   else
     g_assert (serialised.size == 0 || serialised.data != NULL);
 
+  /* Depending on the native alignment requirements of the machine, the
+   * compiler will insert either 3 or 7 padding bytes after the char.
+   * This will result in the sizeof() the struct being 12 or 16.
+   * Subtract 9 to get 3 or 7 which is a nice bitmask to apply to get
+   * the alignment bits that we "care about" being zero: in the
+   * 4-aligned case, we care about 2 bits, and in the 8-aligned case, we
+   * care about 3 bits.
+   */
+  alignment &= sizeof (struct {
+                         char a;
+                         union {
+                           guint64 x;
+                           void *y;
+                           gdouble z;
+                         } b;
+                       }
+                      ) - 9;
+
   g_assert_cmpint (alignment & (gsize) serialised.data, ==, 0);
 }