From d0c9c080b8623e2627c22e5ac4fdaca6585a71c1 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Tue, 17 Dec 2024 17:39:23 +0000 Subject: [PATCH] gbytes: Squash data to `NULL` if length is zero MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This used to happen consistently before !4290, but that MR changed it so that `data` could be non-`NULL` if `size == 0` if the new inline code path is taken. While users of `GBytes` shouldn’t be dereferencing the data if the bytes’ length is zero, it’s definitely safer to make sure the data is `NULL` in that case. This shouldn’t break the expectations of any third party code because it’s restoring the behaviour from before !4290. Signed-off-by: Philip Withnall Fixes: #3562 --- glib/gbytes.c | 2 +- glib/tests/bytes.c | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/glib/gbytes.c b/glib/gbytes.c index a3647caee..1fd3078d9 100644 --- a/glib/gbytes.c +++ b/glib/gbytes.c @@ -128,7 +128,7 @@ g_bytes_new (gconstpointer data, GBytesInline *bytes; bytes = g_malloc (sizeof *bytes + size); - bytes->bytes.data = data != NULL ? bytes->inline_data : NULL; + bytes->bytes.data = (data != NULL && size > 0) ? bytes->inline_data : NULL; bytes->bytes.size = size; bytes->bytes.free_func = NULL; bytes->bytes.user_data = NULL; diff --git a/glib/tests/bytes.c b/glib/tests/bytes.c index 7d432fdee..ac6d4b1d1 100644 --- a/glib/tests/bytes.c +++ b/glib/tests/bytes.c @@ -457,6 +457,14 @@ test_null (void) g_assert_null (data); g_assert_cmpuint (size, ==, 0); + + bytes = g_bytes_new ("some data which shouldn't be touched", 0); + g_assert_null (g_bytes_get_data (bytes, NULL)); + + data = g_bytes_unref_to_data (bytes, &size); + + g_assert_null (data); + g_assert_cmpuint (size, ==, 0); } static void