GVariant: One more FreeBSD fix

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.
This commit is contained in:
Ryan Lortie 2010-05-27 11:32:34 -04:00
parent 271997deb5
commit e7927faf17

View File

@ -129,7 +129,7 @@ static void
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);
@ -157,6 +157,16 @@ g_variant_serialised_check (GVariantSerialised serialised)
}
) - 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);
}