[gjs] arg: don't crash when asked to convert a null strv to an array



commit c86f34c8a09f103731d2b5c11ebc9c0402e1e85f
Author: Cosimo Cecchi <cosimo endlessm com>
Date:   Mon Nov 28 14:41:15 2016 +0100

    arg: don't crash when asked to convert a null strv to an array
    
    In that case, return an empty array.
    
    https://phabricator.endlessm.com/T14337
    
    https://bugzilla.gnome.org/show_bug.cgi?id=775679

 gi/arg.cpp                                |    8 +++++++-
 installed-tests/js/testEverythingBasic.js |    5 +++++
 2 files changed, 12 insertions(+), 1 deletions(-)
---
diff --git a/gi/arg.cpp b/gi/arg.cpp
index f111696..2e33520 100644
--- a/gi/arg.cpp
+++ b/gi/arg.cpp
@@ -572,7 +572,13 @@ gjs_array_from_strv(JSContext             *context,
     guint i;
     JS::AutoValueVector elems(context);
 
-    for (i = 0; strv[i] != NULL; i++) {
+    /* We treat a NULL strv as an empty array, since this function should always
+     * set an array value when returning true.
+     * Another alternative would be to set value_p to JS::NullValue, but clients
+     * would need to always check for both an empty array and null if that was
+     * the case.
+     */
+    for (i = 0; strv != NULL && strv[i] != NULL; i++) {
         elems.growBy(1);
         if (!gjs_string_from_utf8(context, strv[i], -1, elems[i]))
             return false;
diff --git a/installed-tests/js/testEverythingBasic.js b/installed-tests/js/testEverythingBasic.js
index de3ef1a..3b19845 100644
--- a/installed-tests/js/testEverythingBasic.js
+++ b/installed-tests/js/testEverythingBasic.js
@@ -601,6 +601,11 @@ describe('Life, the Universe and Everything', function () {
         }
     });
 
+    it('correctly converts a NULL strv in a GValue to an empty array', function() {
+        let v = Regress.test_null_strv_in_gvalue();
+        expect(v.length).toEqual(0);
+    });
+
     describe('wrong type for GObject', function () {
         let wrongObject, wrongBoxed, subclassObject;
         beforeEach(function () {


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