From 695cdcdba402d1a4635a01aef6327f15282c9bbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Thu, 30 Jan 2025 18:47:53 +0100 Subject: [PATCH] glib/tests/gvariant: Not try to memcpy NULL data It's undefined behavior --- glib/tests/gvariant.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/glib/tests/gvariant.c b/glib/tests/gvariant.c index e215e9861..ebdd559c3 100644 --- a/glib/tests/gvariant.c +++ b/glib/tests/gvariant.c @@ -2325,10 +2325,19 @@ test_byteswap (void) * This might not work because it’s not necessarily possible to make an * arbitrary random variant non-normal. Adding a single zero byte to the end * often makes something non-normal but still readable. */ - three_size_copy = three.size + 1; - three_data_copy = g_malloc (three_size_copy); - memcpy (three_data_copy, three.data, three.size); - three_data_copy[three.size] = '\0'; + if (three.data != NULL) + { + g_assert_cmpuint (three.size, !=, 0); + + three_size_copy = three.size + 1; + three_data_copy = g_malloc (three_size_copy); + memcpy (three_data_copy, three.data, three.size); + three_data_copy[three.size] = '\0'; + } + else + { + g_assert_cmpuint (three.size, ==, 0); + } three_variant = g_variant_new_from_data (G_VARIANT_TYPE (g_variant_type_info_get_type_string (three.type_info)), three_data_copy, three_size_copy, FALSE, NULL, NULL);