[gjs] fundamental: Fix crash with subclassed fundamental with no introspection



commit d3c52255ff2094584b392e46a1977e7a1f1453b2
Author: Lionel Landwerlin <llandwerlin gmail com>
Date:   Sat Jan 2 00:31:49 2016 +0100

    fundamental: Fix crash with subclassed fundamental with no introspection
    
    We could have a fundamental type A derived by a subclass B. Class A happens
    to be introspected, but B isn't. This is currently triggering a crash.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=760057

 gi/fundamental.cpp                    |   14 ++++++++++++--
 installed-tests/js/testFundamental.js |    4 ++++
 2 files changed, 16 insertions(+), 2 deletions(-)
---
diff --git a/gi/fundamental.cpp b/gi/fundamental.cpp
index 56c7121..fd7ea16 100644
--- a/gi/fundamental.cpp
+++ b/gi/fundamental.cpp
@@ -619,8 +619,15 @@ gjs_lookup_fundamental_prototype_from_gtype(JSContext *context,
     GIObjectInfo *info;
     JSObject *proto;
 
-    info = (GIObjectInfo *) g_irepository_find_by_gtype(g_irepository_get_default(),
-                                                        gtype);
+    /* A given gtype might not have any definition in the introspection
+     * data. If that's the case, try to look for a definition of any of the
+     * parent type. */
+    while ((info = (GIObjectInfo *)
+            g_irepository_find_by_gtype(g_irepository_get_default(),
+                                        gtype)) == NULL &&
+           gtype != G_TYPE_INVALID)
+        gtype = g_type_parent(gtype);
+
     proto = gjs_lookup_fundamental_prototype(context, info, gtype);
     if (info)
         g_base_info_unref((GIBaseInfo*)info);
@@ -747,6 +754,9 @@ gjs_object_from_g_fundamental(JSContext    *context,
     JS::RootedObject proto(context,
         gjs_lookup_fundamental_prototype_from_gtype(context,
                                                     G_TYPE_FROM_INSTANCE(gfundamental)));
+    if (!proto)
+        return NULL;
+
     JS::RootedObject global(context, gjs_get_import_global(context));
     object = JS_NewObjectWithGivenProto(context, JS_GetClass(proto), proto,
                                         global);
diff --git a/installed-tests/js/testFundamental.js b/installed-tests/js/testFundamental.js
index ff94a23..fc08de0 100644
--- a/installed-tests/js/testFundamental.js
+++ b/installed-tests/js/testFundamental.js
@@ -4,4 +4,8 @@ describe('Fundamental type support', function () {
     it('constructs a subtype of a fundamental type', function () {
         expect(() => new Regress.TestFundamentalSubObject('plop')).not.toThrow();
     });
+
+    it('constructs a subtype of a hidden (no introspection data) fundamental type', function() {
+        expect(() => Regress.test_create_fundamental_hidden_class_instance()).not.toThrow();
+    });
 });


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