gbytes: Squash data to NULL if length is zero

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 <pwithnall@gnome.org>

Fixes: #3562
This commit is contained in:
Philip Withnall 2024-12-17 17:39:23 +00:00
parent bc56578a08
commit d0c9c080b8
No known key found for this signature in database
GPG Key ID: C5C42CFB268637CA
2 changed files with 9 additions and 1 deletions

View File

@ -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;

View File

@ -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