]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
#18113: avoid segfault if Py_XDECREF triggers code that calls set_panel_userptr again
authorAndrew Kuchling <amk@amk.ca>
Sat, 22 Jun 2013 16:33:05 +0000 (12:33 -0400)
committerAndrew Kuchling <amk@amk.ca>
Sat, 22 Jun 2013 16:33:05 +0000 (12:33 -0400)
Problem noted & original patch by Serhiy Storchaka; I tweaked the patch a bit.

Lib/test/test_curses.py
Modules/_curses_panel.c

index 387a1852d9d1e1bccc33eba27fd67e0a07afc9c8..fcf9618c5bcf6b6e4b9ebe6ff65ad5d1dbdc85fe 100644 (file)
@@ -262,6 +262,14 @@ def test_userptr_memory_leak(stdscr):
     if sys.getrefcount(obj) != nrefs:
         raise RuntimeError, "set_userptr leaked references"
 
+def test_userptr_segfault(stdscr):
+    panel = curses.panel.new_panel(stdscr)
+    class A:
+        def __del__(self):
+            panel.set_userptr(None)
+    panel.set_userptr(A())
+    panel.set_userptr(None)
+
 def test_resize_term(stdscr):
     if hasattr(curses, 'resizeterm'):
         lines, cols = curses.LINES, curses.COLS
@@ -281,6 +289,7 @@ def main(stdscr):
         window_funcs(stdscr)
         test_userptr_without_set(stdscr)
         test_userptr_memory_leak(stdscr)
+        test_userptr_segfault(stdscr)
         test_resize_term(stdscr)
         test_issue6243(stdscr)
     finally:
index dd231e767fa1bf0895d7fb47f265bc10b9758bd0..bdd5cf0177e918329336d437de291ddd6793064f 100644 (file)
@@ -294,12 +294,17 @@ static PyObject *
 PyCursesPanel_set_panel_userptr(PyCursesPanelObject *self, PyObject *obj)
 {
     PyObject *oldobj;
+    int rc;
     PyCursesInitialised;
+    Py_INCREF(obj);
     oldobj = (PyObject *) panel_userptr(self->pan);
+    rc = set_panel_userptr(self->pan, (void*)obj);
+    if (rc == ERR) {
+        /* In case of an ncurses error, decref the new object again */
+        Py_DECREF(obj);
+    }
     Py_XDECREF(oldobj);
-    Py_INCREF(obj);
-    return PyCursesCheckERR(set_panel_userptr(self->pan, (void*)obj),
-                            "set_panel_userptr");
+    return PyCursesCheckERR(rc, "set_panel_userptr");
 }
 
 static PyObject *