diff options
author | Philip Withnall <withnall@endlessm.com> | 2018-11-06 11:43:43 +0000 |
---|---|---|
committer | Philip Withnall <withnall@endlessm.com> | 2018-11-06 12:49:25 +0000 |
commit | 409ff69bd1509499637f7088ef00b370ed703ea6 (patch) | |
tree | d2d812282baf7daa3e73d592cd3effcfb1b422aa /glib/gvariant-serialiser.c | |
parent | 7b0f2e0e348a558b821a27212b82669f969e1526 (diff) | |
download | glib-409ff69bd1509499637f7088ef00b370ed703ea6.tar.gz |
gvariant: Re-use g_variant_serialised_check() to check alignment
Rather than duplicating the alignment checks when constructing a new
GVariant, re-use the alignment checks from GVariantSerialised. This
ensures that the same checks are done everywhere in the GVariant code.
Signed-off-by: Philip Withnall <withnall@endlessm.com>
https://gitlab.gnome.org/GNOME/glib/issues/1342
Diffstat (limited to 'glib/gvariant-serialiser.c')
-rw-r--r-- | glib/gvariant-serialiser.c | 32 |
1 files changed, 17 insertions, 15 deletions
diff --git a/glib/gvariant-serialiser.c b/glib/gvariant-serialiser.c index ccf96b4aa..83e9d85b8 100644 --- a/glib/gvariant-serialiser.c +++ b/glib/gvariant-serialiser.c @@ -127,20 +127,24 @@ * * Checks @serialised for validity according to the invariants described * above. + * + * Returns: %TRUE if @serialised is valid; %FALSE otherwise */ -static void +gboolean g_variant_serialised_check (GVariantSerialised serialised) { gsize fixed_size; guint alignment; - g_assert (serialised.type_info != NULL); + if (serialised.type_info == NULL) + return FALSE; g_variant_type_info_query (serialised.type_info, &alignment, &fixed_size); - if (fixed_size) - g_assert_cmpint (serialised.size, ==, fixed_size); - else - g_assert (serialised.size == 0 || serialised.data != NULL); + if (fixed_size != 0 && serialised.size != fixed_size) + return FALSE; + else if (fixed_size == 0 && + !(serialised.size == 0 || serialised.data != NULL)) + return FALSE; /* Depending on the native alignment requirements of the machine, the * compiler will insert either 3 or 7 padding bytes after the char. @@ -167,10 +171,8 @@ g_variant_serialised_check (GVariantSerialised serialised) * 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); + return (serialised.size <= alignment || + (alignment & (gsize) serialised.data) == 0); } /* < private > @@ -1355,7 +1357,7 @@ gvs_variant_is_normal (GVariantSerialised value) gsize g_variant_serialised_n_children (GVariantSerialised serialised) { - g_variant_serialised_check (serialised); + g_assert (g_variant_serialised_check (serialised)); DISPATCH_CASES (serialised.type_info, @@ -1392,7 +1394,7 @@ g_variant_serialised_get_child (GVariantSerialised serialised, { GVariantSerialised child; - g_variant_serialised_check (serialised); + g_assert (g_variant_serialised_check (serialised)); if G_LIKELY (index_ < g_variant_serialised_n_children (serialised)) { @@ -1400,7 +1402,7 @@ g_variant_serialised_get_child (GVariantSerialised serialised, child = gvs_/**/,/**/_get_child (serialised, index_); g_assert (child.size || child.data == NULL); - g_variant_serialised_check (child); + g_assert (g_variant_serialised_check (child)); return child; ) @@ -1441,7 +1443,7 @@ g_variant_serialiser_serialise (GVariantSerialised serialised, const gpointer *children, gsize n_children) { - g_variant_serialised_check (serialised); + g_assert (g_variant_serialised_check (serialised)); DISPATCH_CASES (serialised.type_info, @@ -1496,7 +1498,7 @@ g_variant_serialised_byteswap (GVariantSerialised serialised) gsize fixed_size; guint alignment; - g_variant_serialised_check (serialised); + g_assert (g_variant_serialised_check (serialised)); if (!serialised.data) return; |