[pygobject] Fix crash when setting property of type object to an incorrect type



commit 2656bc47ca1219b329066da1c2c58018ae624866
Author: Simon Feltman <sfeltman src gnome org>
Date:   Thu Mar 7 18:07:17 2013 -0800

    Fix crash when setting property of type object to an incorrect type
    
    Add type check when marshaling an object from Python for GObject types.
    Add PyGObject_Type as part of the pygobject API to check for this.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=695420

 gi/_gobject/gobjectmodule.c     |    3 ++-
 gi/_gobject/pygobject.h         |    2 ++
 gi/pygi-marshal-from-py.c       |    8 ++++++++
 tests/test_object_marshaling.py |    4 ++++
 4 files changed, 16 insertions(+), 1 deletions(-)
---
diff --git a/gi/_gobject/gobjectmodule.c b/gi/_gobject/gobjectmodule.c
index c49b412..359c2c7 100644
--- a/gi/_gobject/gobjectmodule.c
+++ b/gi/_gobject/gobjectmodule.c
@@ -2086,7 +2086,8 @@ struct _PyGObject_Functions pygobject_api_functions = {
   pyglib_option_group_new,
   pyg_type_from_object_strict,
 
-  pygobject_new_full
+  pygobject_new_full,
+  &PyGObject_Type
 };
 
 /* for addon libraries ... */
diff --git a/gi/_gobject/pygobject.h b/gi/_gobject/pygobject.h
index cfd88d7..15b8807 100644
--- a/gi/_gobject/pygobject.h
+++ b/gi/_gobject/pygobject.h
@@ -191,6 +191,7 @@ struct _PyGObject_Functions {
     GType (* type_from_object_strict) (PyObject *obj, gboolean strict);
 
     PyObject *(* newgobj_full)(GObject *obj, gboolean steal, gpointer g_class);
+    PyTypeObject *object_type;
 };
 
 #ifndef _INSIDE_PYGOBJECT_
@@ -206,6 +207,7 @@ struct _PyGObject_Functions *_PyGObject_API;
 #define pygobject_lookup_class      (_PyGObject_API->lookup_class)
 #define pygobject_new               (_PyGObject_API->newgobj)
 #define pygobject_new_full          (_PyGObject_API->newgobj_full)
+#define PyGObject_Type              (*_PyGObject_API->object_type)
 #define pyg_closure_new             (_PyGObject_API->closure_new)
 #define pygobject_watch_closure     (_PyGObject_API->object_watch_closure)
 #define pyg_closure_set_exception_handler (_PyGObject_API->closure_set_exception_handler)
diff --git a/gi/pygi-marshal-from-py.c b/gi/pygi-marshal-from-py.c
index 9f46977..1111545 100644
--- a/gi/pygi-marshal-from-py.c
+++ b/gi/pygi-marshal-from-py.c
@@ -1842,6 +1842,14 @@ pygi_marshal_from_py_gobject (PyObject *py_arg, /*in*/
         return TRUE;
     }
 
+    if (!pygobject_check (py_arg, &PyGObject_Type)) {
+        PyObject *repr = PyObject_Repr (py_arg);
+        PyErr_Format(PyExc_TypeError, "expected GObject but got %s",
+                     PYGLIB_PyUnicode_AsString (repr));
+        Py_DECREF (repr);
+        return FALSE;
+    }
+
     gobj = pygobject_get (py_arg);
     if (transfer == GI_TRANSFER_EVERYTHING) {
         /* An easy case of adding a new ref that the caller will take ownership of.
diff --git a/tests/test_object_marshaling.py b/tests/test_object_marshaling.py
index 3434959..2432b44 100644
--- a/tests/test_object_marshaling.py
+++ b/tests/test_object_marshaling.py
@@ -611,3 +611,7 @@ class TestPropertyHoldingObject(unittest.TestCase):
         # Clearing should pull it back down
         holder.set_property('some-object', None)
         self.assertEqual(held.__grefcount__, 1)
+
+    def test_set_object_property_to_invalid_type(self):
+        obj = GIMarshallingTests.PropertiesObject()
+        self.assertRaises(TypeError, obj.set_property, 'some-object', 'not_an_object')


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