[glib] GVariant: Check for size == 0 in get_bytestring



commit 235820d0ef817d549844fa898349e31f28fb833d
Author: Ryan Lortie <desrt desrt ca>
Date:   Tue Sep 14 14:55:38 2010 -0400

    GVariant: Check for size == 0 in get_bytestring
    
    And add a test from David that shows the problem.
    
    Closes #629698

 glib/gvariant.c       |    2 +-
 glib/tests/gvariant.c |    8 ++++++++
 2 files changed, 9 insertions(+), 1 deletions(-)
---
diff --git a/glib/gvariant.c b/glib/gvariant.c
index 133bfa1..f9eb851 100644
--- a/glib/gvariant.c
+++ b/glib/gvariant.c
@@ -1368,7 +1368,7 @@ g_variant_get_bytestring (GVariant *value)
   string = g_variant_get_data (value);
   size = g_variant_get_size (value);
 
-  if (string[size - 1] == '\0')
+  if (size && string[size - 1] == '\0')
     return string;
   else
     return "";
diff --git a/glib/tests/gvariant.c b/glib/tests/gvariant.c
index 7e228ac..74eee40 100644
--- a/glib/tests/gvariant.c
+++ b/glib/tests/gvariant.c
@@ -3819,6 +3819,8 @@ test_bytestring (void)
   GVariant *value;
   gchar **strv;
   gchar *str;
+  const gchar *const_str;
+  GVariant *untrusted_empty;
 
   strv = g_strsplit (test_string, ",", 0);
 
@@ -3884,6 +3886,12 @@ test_bytestring (void)
   g_variant_get_child (value, 3, "^&ay", &str);
   g_assert_cmpstr (str, ==, "foo");
   g_variant_unref (value);
+
+  untrusted_empty = g_variant_new_from_data (G_VARIANT_TYPE ("ay"), NULL, 0, FALSE, NULL, NULL);
+  value = g_variant_get_normal_form (untrusted_empty);
+  const_str = g_variant_get_bytestring (value);
+  g_variant_unref (value);
+  g_variant_unref (untrusted_empty);
 }
 
 int



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