[pygobject] Fix segfault when accessing __grefcount__ before creating the GObject



commit 96f14989baea76fe8692f10c1a37e2dfc45fecbf
Author: Steve FrÃcinaux <code istique net>
Date:   Wed Apr 4 15:30:55 2012 +0200

    Fix segfault when accessing __grefcount__ before creating the GObject
    
    When creating a new instance using Type() and trying to access
    __grefcount__ before calling the subclass's __init__ function, there
    used to be a segmentation fault because we were trying to access the
    not yet created object. Now raise a proper exception instead.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=640434
    
    Co-authored-by: Martin Pitt <martinpitt gnome org>

 gi/_gobject/pygobject.c |    4 ++++
 tests/test_gobject.py   |   11 +++++++++++
 2 files changed, 15 insertions(+), 0 deletions(-)
---
diff --git a/gi/_gobject/pygobject.c b/gi/_gobject/pygobject.c
index 92dc573..66b8ae6 100644
--- a/gi/_gobject/pygobject.c
+++ b/gi/_gobject/pygobject.c
@@ -2058,6 +2058,10 @@ pygobject_get_dict(PyGObject *self, void *closure)
 static PyObject *
 pygobject_get_refcount(PyGObject *self, void *closure)
 {
+    if (self->obj == NULL) {
+	PyErr_Format(PyExc_TypeError, "GObject instance is not yet created");
+	return NULL;
+    }
     return PYGLIB_PyLong_FromLong(self->obj->ref_count);
 }
 
diff --git a/tests/test_gobject.py b/tests/test_gobject.py
index 95227bb..80725b3 100644
--- a/tests/test_gobject.py
+++ b/tests/test_gobject.py
@@ -154,6 +154,17 @@ class TestReferenceCounting(unittest.TestCase):
         obj.release()
         self.assertEquals(obj.__grefcount__, 1)
 
+    def testUninitializedObject(self):
+        class Obj(GObject.GObject):
+            def __init__(self):
+                x = self.__grefcount__
+                super(Obj, self).__init__()
+                assert x >= 0  # quiesce pyflakes
+
+        # Accessing __grefcount__ before the object is initialized is wrong.
+        # Ensure we get a proper exception instead of a crash.
+        self.assertRaises(TypeError, Obj)
+
 
 class A(GObject.GObject):
     def __init__(self):



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