File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -264,6 +264,14 @@ def test_userptr_memory_leak(stdscr):
264264 if sys .getrefcount (obj ) != nrefs :
265265 raise RuntimeError ("set_userptr leaked references" )
266266
267+ def test_userptr_segfault (stdscr ):
268+ panel = curses .panel .new_panel (stdscr )
269+ class A :
270+ def __del__ (self ):
271+ panel .set_userptr (None )
272+ panel .set_userptr (A ())
273+ panel .set_userptr (None )
274+
267275def test_resize_term (stdscr ):
268276 if hasattr (curses , 'resizeterm' ):
269277 lines , cols = curses .LINES , curses .COLS
@@ -330,6 +338,7 @@ def main(stdscr):
330338 window_funcs (stdscr )
331339 test_userptr_without_set (stdscr )
332340 test_userptr_memory_leak (stdscr )
341+ test_userptr_segfault (stdscr )
333342 test_resize_term (stdscr )
334343 test_issue6243 (stdscr )
335344 test_unget_wch (stdscr )
Original file line number Diff line number Diff line change @@ -323,12 +323,17 @@ static PyObject *
323323PyCursesPanel_set_panel_userptr (PyCursesPanelObject * self , PyObject * obj )
324324{
325325 PyObject * oldobj ;
326+ int rc ;
326327 PyCursesInitialised ;
328+ Py_INCREF (obj );
327329 oldobj = (PyObject * ) panel_userptr (self -> pan );
330+ rc = set_panel_userptr (self -> pan , (void * )obj );
331+ if (rc == ERR ) {
332+ /* In case of an ncurses error, decref the new object again */
333+ Py_DECREF (obj );
334+ }
328335 Py_XDECREF (oldobj );
329- Py_INCREF (obj );
330- return PyCursesCheckERR (set_panel_userptr (self -> pan , (void * )obj ),
331- "set_panel_userptr" );
336+ return PyCursesCheckERR (rc , "set_panel_userptr" );
332337}
333338
334339static PyObject *
You can’t perform that action at this time.
0 commit comments