FreeBSD's malloc() sometimes returns unaligned memory if you are
requesting small sizes. This can get GVariant into trouble. For
example, consider the type "mmi" containing the value "just nothing".
According to the type signature, the memory containing this should be
aligned to a boundary of 4 since it might contain an int. The
serialised size of this value is 1 byte, however, and when you ask
FreeBSD to allocate memory of that size, it knows you can't put an int
into it so it doesn't bother aligning it.
This patch modifies the GVariant serialiser to not assert the alignment
constraint in the case that the size of the serialised data is smaller
than its own alignment requirement.
g_variant_serialised_check (GVariantSerialised serialised)
{
gsize fixed_size;
- guint alignment;
+ gsize alignment;
g_assert (serialised.type_info != NULL);
g_variant_type_info_query (serialised.type_info, &alignment, &fixed_size);
}
) - 9;
+ /* Some OSes (FreeBSD is a known example) have a malloc() that returns
+ * unaligned memory if you request small sizes. 'malloc (1);', for
+ * example, has been seen to return pointers aligned to 6 mod 16.
+ *
+ * Check if this is a small allocation and return without enforcing
+ * the alignment assertion if this is the case.
+ */
+ if (serialised.size <= alignment)
+ return;
+
g_assert_cmpint (alignment & (gsize) serialised.data, ==, 0);
}