diff --git a/glib/gvariant-core.c b/glib/gvariant-core.c index f823e0141..dc20c3254 100644 --- a/glib/gvariant-core.c +++ b/glib/gvariant-core.c @@ -882,11 +882,24 @@ g_variant_get_data (GVariant *value) GBytes * g_variant_get_data_as_bytes (GVariant *value) { + const gchar *bytes_data; + const gchar *data; + gsize bytes_size; + gsize size; + g_variant_lock (value); g_variant_ensure_serialised (value); g_variant_unlock (value); - return g_bytes_ref (value->contents.serialised.bytes); + bytes_data = g_bytes_get_data (value->contents.serialised.bytes, &bytes_size); + data = value->contents.serialised.data; + size = value->size; + + if (data == bytes_data && size == bytes_size) + return g_bytes_ref (value->contents.serialised.bytes); + else + return g_bytes_new_from_bytes (value->contents.serialised.bytes, + data - bytes_data, size); } diff --git a/glib/tests/gvariant.c b/glib/tests/gvariant.c index d2d09d97a..4e531c864 100644 --- a/glib/tests/gvariant.c +++ b/glib/tests/gvariant.c @@ -4258,6 +4258,7 @@ static void test_gbytes (void) { GVariant *a; + GVariant *tuple; GBytes *bytes; GBytes *bytes2; const guint8 values[5] = { 1, 2, 3, 4, 5 }; @@ -4279,9 +4280,19 @@ test_gbytes (void) bytes = g_bytes_new (&values, 5); g_assert (g_bytes_equal (bytes, bytes2)); + g_bytes_unref (bytes); + g_bytes_unref (bytes2); + + tuple = g_variant_new_parsed ("['foo', 'bar']"); + bytes = g_variant_get_data_as_bytes (tuple); /* force serialisation */ + a = g_variant_get_child_value (tuple, 1); + bytes2 = g_variant_get_data_as_bytes (a); + g_assert (!g_bytes_equal (bytes, bytes2)); g_bytes_unref (bytes); g_bytes_unref (bytes2); + g_variant_unref (a); + g_variant_unref (tuple); } int