pygtk r2999 - in trunk: . gtk tests



Author: paulp
Date: Tue Jun 24 18:42:25 2008
New Revision: 2999
URL: http://svn.gnome.org/viewvc/pygtk?rev=2999&view=rev

Log:
2008-06-24  Paul Pogonyshev  <pogonyshev gmx net>

	* gtk/gtktreeview.override (_wrap_gtk_tree_model_tp_getitem): Fix
	logic in negative index branch to avoid refcount corruption.
	(_wrap_gtk_tree_model_tp_setitem): Likewise (bug #537459).

	* tests/test_liststore.py (ListStoreTest.testNegativeIndexGet)
	(ListStoreTest.testNegativeIndexSet): Two new tests for the bug.


Modified:
   trunk/ChangeLog
   trunk/gtk/gtktreeview.override
   trunk/tests/test_liststore.py

Modified: trunk/gtk/gtktreeview.override
==============================================================================
--- trunk/gtk/gtktreeview.override	(original)
+++ trunk/gtk/gtktreeview.override	Tue Jun 24 18:42:25 2008
@@ -987,7 +987,7 @@
 static PyObject *
 _wrap_gtk_tree_model_tp_getitem(PyGObject *self, PyObject *item)
 {
-    GtkTreePath *path;
+    GtkTreePath *path = NULL;
     GtkTreeIter iter;
     PyObject *ret = NULL;
 
@@ -1000,17 +1000,24 @@
 	int value = PyInt_AsLong(item);
 
 	if (value < 0) {
-	    /* Since value is always negative at this point,
+            PyObject *inverse;
+
+            /* Since value is always negative at this point,
 	     * we need to do an invertion.
 	     */
-	    value = _wrap_gtk_tree_model_tp_length(self) - -value;
-	    
-	    Py_DECREF(item);
-	    item = PyInt_FromLong(value);
+            value = _wrap_gtk_tree_model_tp_length(self) - -value;
+            inverse = PyInt_FromLong(value);
+            if (!inverse)
+                return NULL;
+
+            path = pygtk_tree_path_from_pyobject(inverse);
+            Py_DECREF(inverse);
 	}
     }
-    
-    path = pygtk_tree_path_from_pyobject(item);
+
+    if (!path)
+        path = pygtk_tree_path_from_pyobject(item);
+
     if (!path) {
         PyErr_SetString(PyExc_TypeError,
                         "could not parse subscript as a tree path");
@@ -1034,23 +1041,29 @@
     if (pyg_boxed_check(item, GTK_TYPE_TREE_ITER)) {
         iter = pyg_boxed_get(item, GtkTreeIter);
     } else {
-        GtkTreePath *path;
+        GtkTreePath *path = NULL;
 
 	if (PyInt_Check(item)) {
 	    int value = PyInt_AsLong(item);
 
 	    if (value < 0) {
-		/* Since value is always negative at this point,
+                PyObject *inverse;
+
+                /* Since value is always negative at this point,
 		 * we need to do an invertion.
 		 */
-		value = _wrap_gtk_tree_model_tp_length(self) - -value;
-	    
-		Py_DECREF(item);
-		item = PyInt_FromLong(value);
+                value = _wrap_gtk_tree_model_tp_length(self) - -value;
+                inverse = PyInt_FromLong(value);
+                if (!inverse)
+                    return -1;
+
+                path = pygtk_tree_path_from_pyobject(inverse);
+                Py_DECREF(inverse);
 	    }
 	}
 
-	path = pygtk_tree_path_from_pyobject(item);
+        if (!path)
+            path = pygtk_tree_path_from_pyobject(item);
 
         if (!path) {
             PyErr_SetString(PyExc_TypeError,

Modified: trunk/tests/test_liststore.py
==============================================================================
--- trunk/tests/test_liststore.py	(original)
+++ trunk/tests/test_liststore.py	Tue Jun 24 18:42:25 2008
@@ -32,5 +32,23 @@
         store.set_default_sort_func(None)
         self.failIf(store.has_default_sort_func())
 
+    # Two tests for bug 537459.  Since it is a memory corruption, we
+    # use a large loop count hoping to trigger a segfault.
+
+    def testNegativeIndexGet(self):
+        store = gtk.ListStore(int)
+        for i in range(200):
+            store.append((i,))
+            self.assertEqual(store[-1][0], i)
+
+    def testNegativeIndexSet(self):
+        store = gtk.ListStore(int)
+        for i in range(200):
+            store.append((i,))
+            del store[-1]
+            self.assert_(len(store) == 0)
+
+
+
 if __name__ == '__main__':
     unittest.main()



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