[gjs] gtype: Don't recurse infinitely while getting a GType



commit df28a9c070459cc90b0ea0db19bf8e0568ac396a
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Tue Oct 2 21:37:36 2012 -0300

    gtype: Don't recurse infinitely while getting a GType
    
    If we pass a plain old JS object to gjs_gtype_get_actual_gtype,
    it will recurse infinitely, getting the constructor. Any object's
    constructor is a Function, and unfortunately Function.prototype.constructor
    returns itself. Fix this by only recursing once.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=685436

 gi/gtype.c |   21 ++++++++++++++-------
 1 files changed, 14 insertions(+), 7 deletions(-)
---
diff --git a/gi/gtype.c b/gi/gtype.c
index 850b926..9c0bb75 100644
--- a/gi/gtype.c
+++ b/gi/gtype.c
@@ -145,9 +145,10 @@ gjs_gtype_create_gtype_wrapper (JSContext *context,
     return object;
 }
 
-GType
-gjs_gtype_get_actual_gtype (JSContext *context,
-                            JSObject  *object)
+static GType
+_gjs_gtype_get_actual_gtype (JSContext *context,
+                             JSObject  *object,
+                             gboolean   recurse)
 {
     GType gtype = G_TYPE_INVALID;
     jsval gtype_val = JSVAL_VOID;
@@ -162,17 +163,23 @@ gjs_gtype_get_actual_gtype (JSContext *context,
      * property on that and hope it's a GType wrapper object */
     if (!JS_GetProperty(context, object, "$gtype", &gtype_val) ||
         !JSVAL_IS_OBJECT(gtype_val)) {
-
         /* OK, so we're not a class. But maybe we're an instance. Check
-           for "constructor" and recurse on that. */
+         * for "constructor" and recurse on that. */
         if (!JS_GetProperty(context, object, "constructor", &gtype_val))
             goto out;
     }
 
-    if (JSVAL_IS_OBJECT(gtype_val))
-        gtype = gjs_gtype_get_actual_gtype(context, JSVAL_TO_OBJECT(gtype_val));
+    if (recurse && JSVAL_IS_OBJECT(gtype_val))
+        gtype = _gjs_gtype_get_actual_gtype(context, JSVAL_TO_OBJECT(gtype_val), FALSE);
 
  out:
     JS_EndRequest(context);
     return gtype;
 }
+
+GType
+gjs_gtype_get_actual_gtype (JSContext *context,
+                            JSObject  *object)
+{
+    return _gjs_gtype_get_actual_gtype(context, object, TRUE);
+}


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