[gjs] Be robust when faced with NULL arrays



commit 36094f3aec9123ee13eb466f13e991bd129a1af1
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Mon Feb 6 17:49:56 2012 +0100

    Be robust when faced with NULL arrays
    
    Some API return NULL to mean an empty array (especially if they
    have an explicit length). Be robust in this cases, and handle such
    empty bytearrays in a way that prevents crashes.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=667069

 gi/arg.c        |    5 +++++
 gjs/byteArray.c |   11 +++++++++--
 2 files changed, 14 insertions(+), 2 deletions(-)
---
diff --git a/gi/arg.c b/gi/arg.c
index 94f7128..dd1259f 100644
--- a/gi/arg.c
+++ b/gi/arg.c
@@ -2058,6 +2058,11 @@ gjs_array_from_boxed_array (JSContext   *context,
     gpointer data = NULL;
     gsize length = 0;
 
+    if (arg->v_pointer == NULL) {
+        *value_p = JSVAL_NULL;
+        return TRUE;
+    }
+
     switch(array_type) {
     case GI_ARRAY_TYPE_BYTE_ARRAY:
         /* GByteArray is just a typedef for GArray internally */
diff --git a/gjs/byteArray.c b/gjs/byteArray.c
index 75df27c..36a7406 100644
--- a/gjs/byteArray.c
+++ b/gjs/byteArray.c
@@ -508,6 +508,7 @@ to_string_func(JSContext *context,
     ByteArrayInstance *priv;
     char *encoding;
     gboolean encoding_is_utf8;
+    gchar *data;
 
     priv = priv_from_js(context, object);
 
@@ -535,6 +536,12 @@ to_string_func(JSContext *context,
         encoding = "UTF-8";
     }
 
+    if (priv->array->len == 0)
+        /* the internal data pointer could be NULL in this case */
+        data = "";
+    else
+        data = priv->array->data;
+
     if (encoding_is_utf8) {
         /* optimization, avoids iconv overhead and runs
          * glib's hardwired utf8-to-utf16
@@ -543,7 +550,7 @@ to_string_func(JSContext *context,
         JSBool ok;
 
         ok = gjs_string_from_utf8(context,
-                                  (char*) priv->array->data,
+                                  data,
                                   priv->array->len,
                                   &retval);
         if (ok)
@@ -557,7 +564,7 @@ to_string_func(JSContext *context,
         char *u16_str;
 
         error = NULL;
-        u16_str = g_convert((char*) priv->array->data,
+        u16_str = g_convert(data,
                            priv->array->len,
                            "UTF-16",
                            encoding,



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