8000 [3.13] bpo-44172: Keep reference to original window in curses subwindow objects (GH-26226) by serhiy-storchaka · Pull Request #133370 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.13] bpo-44172: Keep reference to original window in curses subwindow objects (GH-26226) #133370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[3.13] bpo-44172: Keep reference to original window in curses subwind…
…ow objects (GH-26226)

The X/Open curses specification[0] and ncurses documentation[1]
both state that subwindows must be deleted before the main window.

Deleting the windows in the wrong order causes a double-free with
NetBSD's curses implementation.

To fix this, keep track of the original window object in the subwindow
object, and keep a reference to the original for the lifetime of
the subwindow.

[0] https://pubs.opengroup.org/onlinepubs/7908799/xcurses/delwin.html
[1] https://invisible-island.net/ncurses/man/curs_window.3x.html
(cherry picked from commit 0af61fe)

Co-authored-by: Michael Forney <mforney@mforney.org>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
  • Loading branch information
michaelforney and serhiy-storchaka committed May 4, 2025
commit a4ebf5d1f7342d4c360a72862f76331a917b7e2f
3 changes: 2 additions & 1 deletion Include/py_curses.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ extern "C" {

/* Type declarations */

typedef struct {
typedef struct PyCursesWindowObject {
PyObject_HEAD
WINDOW *win;
char *encoding;
struct PyCursesWindowObject *orig;
} PyCursesWindowObject;

#define PyCursesWindow_Check(v) Py_IS_TYPE((v), &PyCursesWindow_Type)
Expand Down
11 changes: 10 additions & 1 deletion Lib/test/test_curses.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from unittest.mock import MagicMock

from test.support import (requires, verbose, SaveSignals, cpython_only,
check_disallow_instantiation, MISSING_C_DOCSTRINGS)
check_disallow_instantiation, MISSING_C_DOCSTRINGS,
gc_collect)
from test.support.import_helper import import_module

# Optionally test curses module. This currently requires that the
Expand Down Expand Up @@ -187,6 +188,14 @@ def test_create_windows(self):
self.assertEqual(win3.getparyx(), (2, 1))
self.assertEqual(win3.getmaxyx(), (6, 11))

def test_subwindows_references(self):
win = curses.newwin(5, 10)
win2 = win.subwin(3, 7)
del win
gc_collect()
del win2
gc_collect()

def test_move_cursor(self):
stdscr = self.stdscr
win = stdscr.subwin(10, 15, 2, 5)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Keep a reference to original :mod:`curses` windows in subwindows so
that the original window does not get deleted before subwindows.
20 changes: 12 additions & 8 deletions Modules/_cursesmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,8 @@ Window_TwoArgNoReturnFunction(wresize, int, "ii;lines,columns")
/* Allocation and deallocation of Window Objects */

static PyObject *
PyCursesWindow_New(WINDOW *win, const char *encoding)
PyCursesWindow_New(WINDOW *win, const char *encoding,
PyCursesWindowObject *orig)
{
PyCursesWindowObject *wo;

Expand Down Expand Up @@ -697,6 +698,8 @@ PyCursesWindow_New(WINDOW *win, const char *encoding)
PyErr_NoMemory();
return NULL;
}
wo->orig = orig;
Py_XINCREF(orig);
return (PyObject *)wo;
}

Expand All @@ -706,6 +709,7 @@ PyCursesWindow_Dealloc(PyCursesWindowObject *wo)
if (wo->win != stdscr) delwin(wo->win);
if (wo->encoding != NULL)
PyMem_Free(wo->encoding);
Py_XDECREF(wo->orig);
PyObject_Free(wo);
}

Expand Down Expand Up @@ -1309,7 +1313,7 @@ _curses_window_derwin_impl(PyCursesWindowObject *self, int group_left_1,
return NULL;
}

return (PyObject *)PyCursesWindow_New(win, NULL);
return (PyObject *)PyCursesWindow_New(win, NULL, self);
}

/*[clinic input]
Expand Down Expand Up @@ -2336,7 +2340,7 @@ _curses_window_subwin_impl(PyCursesWindowObject *self, int group_left_1,
return NULL;
}

return (PyObject *)PyCursesWindow_New(win, self->encoding);
return (PyObject *)PyCursesWindow_New(win, self->encoding, self);
}

/*[clinic input]
Expand Down Expand Up @@ -3084,7 +3088,7 @@ _curses_getwin(PyObject *module, PyObject *file)
PyErr_SetString(PyCursesError, catchall_NULL);
goto error;
}
res = PyCursesWindow_New(win, NULL);
res = PyCursesWindow_New(win, NULL, NULL);

error:
fclose(fp);
Expand Down Expand Up @@ -3257,7 +3261,7 @@ _curses_initscr_impl(PyObject *module)

if (initialised) {
wrefresh(stdscr);
return (PyObject *)PyCursesWindow_New(stdscr, NULL);
return (PyObject *)PyCursesWindow_New(stdscr, NULL, NULL);
}

win = initscr();
Expand Down Expand Up @@ -3349,7 +3353,7 @@ _curses_initscr_impl(PyObject *module)
SetDictInt("LINES", LINES);
SetDictInt("COLS", COLS);

winobj = (PyCursesWindowObject *)PyCursesWindow_New(win, NULL);
winobj = (PyCursesWindowObject *)PyCursesWindow_New(win, NULL, NULL);
screen_encoding = winobj->encoding;
return (PyObject *)winobj;
}
Expand Down Expand Up @@ -3728,7 +3732,7 @@ _curses_newpad_impl(PyObject *module, int nlines, int ncols)
return NULL;
}

return (PyObject *)PyCursesWindow_New(win, NULL);
return (PyObject *)PyCursesWindow_New(win, NULL, NULL);
}

/*[clinic input]
Expand Down Expand Up @@ -3767,7 +3771,7 @@ _curses_newwin_impl(PyObject *module, int nlines, int ncols,
return NULL;
}

return (PyObject *)PyCursesWindow_New(win, NULL);
return (PyObject *)PyCursesWindow_New(win, NULL, NULL);
}

/*[clinic input]
Expand Down
Loading
0