gresource: Fix handling of zero-sized compressed resource entries

The zlib `GConverter` can’t handle an output buffer of size 0.

Add tests.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>

Fixes: #1560
This commit is contained in:
Philip Withnall 2020-11-14 19:02:31 +00:00
parent 1c290acd74
commit 353020928c
4 changed files with 71 additions and 1 deletions

View File

@ -800,7 +800,9 @@ g_resource_lookup_data (GResource *resource,
if (!do_lookup (resource, path, lookup_flags, &size, &flags, &data, &data_size, error))
return NULL;
if (flags & G_RESOURCE_FLAGS_COMPRESSED)
if (size == 0)
return g_bytes_new_with_free_func ("", 0, (GDestroyNotify) g_resource_unref, g_resource_ref (resource));
else if (flags & G_RESOURCE_FLAGS_COMPRESSED)
{
char *uncompressed, *d;
const char *s;

0
gio/tests/empty.txt Normal file
View File

View File

@ -68,6 +68,15 @@ test_resource (GResource *resource)
g_assert_cmpint (size, ==, 6);
g_assert_cmpuint (flags, ==, G_RESOURCE_FLAGS_COMPRESSED);
found = g_resource_get_info (resource,
"/empty.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert_true (found);
g_assert_no_error (error);
g_assert_cmpint (size, ==, 0);
g_assert_cmpuint (flags, ==, G_RESOURCE_FLAGS_COMPRESSED);
found = g_resource_get_info (resource,
"/a_prefix/test2.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
@ -105,6 +114,14 @@ test_resource (GResource *resource)
g_assert_no_error (error);
g_bytes_unref (data);
data = g_resource_lookup_data (resource,
"/empty.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert_cmpuint (g_bytes_get_size (data), ==, 0);
g_assert_no_error (error);
g_bytes_unref (data);
for (i = 0; i < G_N_ELEMENTS (not_found_paths); i++)
{
in = g_resource_open_stream (resource,
@ -136,6 +153,24 @@ test_resource (GResource *resource)
g_assert_no_error (error);
g_clear_object (&in);
in = g_resource_open_stream (resource,
"/empty.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert_no_error (error);
g_assert_nonnull (in);
success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
&size,
NULL, &error);
g_assert_no_error (error);
g_assert_true (success);
g_assert_cmpint (size, ==, 0);
g_input_stream_close (in, NULL, &error);
g_assert_no_error (error);
g_clear_object (&in);
data = g_resource_lookup_data (resource,
"/a_prefix/test2.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
@ -398,6 +433,14 @@ test_resource_registered (void)
g_assert_cmpint (size, ==, 6);
g_assert (flags == (G_RESOURCE_FLAGS_COMPRESSED));
found = g_resources_get_info ("/empty.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert_no_error (error);
g_assert_true (found);
g_assert_cmpint (size, ==, 0);
g_assert (flags == (G_RESOURCE_FLAGS_COMPRESSED));
found = g_resources_get_info ("/a_prefix/test2.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
@ -440,6 +483,30 @@ test_resource_registered (void)
g_assert_no_error (error);
g_clear_object (&in);
data = g_resources_lookup_data ("/empty.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert_no_error (error);
g_assert_cmpuint (g_bytes_get_size (data), ==, 0);
g_bytes_unref (data);
in = g_resources_open_stream ("/empty.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert_no_error (error);
g_assert_nonnull (in);
success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
&size,
NULL, &error);
g_assert_no_error (error);
g_assert_true (success);
g_assert_cmpint (size, ==, 0);
g_input_stream_close (in, NULL, &error);
g_assert_no_error (error);
g_clear_object (&in);
data = g_resources_lookup_data ("/a_prefix/test2.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);

View File

@ -4,6 +4,7 @@
<file >test-generated.txt</file>
<file compressed="true">test1.txt</file>
<file preprocess="xml-stripblanks">test.gresource.xml</file>
<file compressed="true">empty.txt</file>
</gresource>
<gresource prefix="/a_prefix">
<file alias="test2-alias.txt">test2.txt</file>