[glib: 1/2] gresource: Fix handling of zero-sized compressed resource entries




commit 353020928cbe842a1901a2490bb3bff881591155
Author: Philip Withnall <pwithnall endlessos org>
Date:   Sat Nov 14 19:02:31 2020 +0000

    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

 gio/gresource.c              |  4 ++-
 gio/tests/empty.txt          |  0
 gio/tests/resources.c        | 67 ++++++++++++++++++++++++++++++++++++++++++++
 gio/tests/test.gresource.xml |  1 +
 4 files changed, 71 insertions(+), 1 deletion(-)
---
diff --git a/gio/gresource.c b/gio/gresource.c
index b7222b8eb..79a49d33d 100644
--- a/gio/gresource.c
+++ b/gio/gresource.c
@@ -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;
diff --git a/gio/tests/empty.txt b/gio/tests/empty.txt
new file mode 100644
index 000000000..e69de29bb
diff --git a/gio/tests/resources.c b/gio/tests/resources.c
index 0749b7d01..325775fd6 100644
--- a/gio/tests/resources.c
+++ b/gio/tests/resources.c
@@ -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);
diff --git a/gio/tests/test.gresource.xml b/gio/tests/test.gresource.xml
index dd08aa006..62a31f496 100644
--- a/gio/tests/test.gresource.xml
+++ b/gio/tests/test.gresource.xml
@@ -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>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]