Skip to content

Commit 7ef0e32

Browse files
committed
gh-154781: Zero the in_wstr() buffer in curses
winnwstr() only writes the terminating null when it stored at least one character, so with n of 0 the result was read from uninitialized memory. Use PyMem_Calloc, like in_wchstr() already does.
1 parent 92efaff commit 7ef0e32

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

Lib/test/test_curses.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,12 @@ def test_in_wstr(self):
514514
self.assertEqual(stdscr.in_wstr(0, 0, len(s)), s)
515515
self.assertIsInstance(stdscr.instr(0, 0, len(s)), bytes)
516516

517+
# Reading no characters gives an empty string, like instr() and
518+
# in_wchstr() do. curses does not terminate the buffer in this case.
519+
stdscr.addstr(0, 0, 'abz')
520+
self.assertEqual(stdscr.in_wstr(0, 0, 0), '')
521+
self.assertEqual(stdscr.in_wstr(0), '')
522+
517523
def test_complexchar(self):
518524
# A complexchar is a styled wide-character cell: str() is its text,
519525
# and the attr and pair attributes are its rendition.

Modules/_cursesmodule.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3850,7 +3850,9 @@ PyCursesWindow_in_wstr(PyObject *op, PyObject *args)
38503850
}
38513851

38523852
n = Py_MIN(n, max_buf_size - 1);
3853-
wchar_t *buf = PyMem_New(wchar_t, n + 1);
3853+
/* Zero the buffer: winnwstr() only writes the terminating null when it
3854+
stored at least one character, and the result is read up to it. */
3855+
wchar_t *buf = PyMem_Calloc(n + 1, sizeof(wchar_t));
38543856
if (buf == NULL) {
38553857
return PyErr_NoMemory();
38563858
}

0 commit comments

Comments
 (0)