Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 40a77c3

Browse files
committed
do not allow reading negative values with getstr()
1 parent 59b6abd commit 40a77c3

3 files changed

Lines changed: 14 additions & 0 deletions

File tree

Lib/test/test_curses.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ def test_window_funcs(self):
163163
if hasattr(curses, 'enclose'):
164164
stdscr.enclose()
165165

166+
self.assertRaises(ValueError, stdscr.getstr, -400)
167+
self.assertRaises(ValueError, stdscr.getstr, 2, 3, -400)
168+
166169

167170
def test_module_funcs(self):
168171
"Test module-level functions"

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Core and Builtins
1313
Library
1414
-------
1515

16+
- In the curses module, raise an error if window.getstr() is passed a negative
17+
value.
18+
1619
- Issue #27758: Fix possible integer overflow in the _csv module for large record
1720
lengths.
1821

Modules/_cursesmodule.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,10 @@ PyCursesWindow_GetStr(PyCursesWindowObject *self, PyObject *args)
12841284
case 1:
12851285
if (!PyArg_ParseTuple(args,"i;n", &n))
12861286
return NULL;
1287+
if (n < 0) {
1288+
PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
1289+
return NULL;
1290+
}
12871291
Py_BEGIN_ALLOW_THREADS
12881292
rtn2 = wgetnstr(self->win, rtn, Py_MIN(n, 1023));
12891293
Py_END_ALLOW_THREADS
@@ -1302,6 +1306,10 @@ PyCursesWindow_GetStr(PyCursesWindowObject *self, PyObject *args)
13021306
case 3:
13031307
if (!PyArg_ParseTuple(args,"iii;y,x,n", &y, &x, &n))
13041308
return NULL;
1309+
if (n < 0) {
1310+
PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
1311+
return NULL;
1312+
}
13051313
#ifdef STRICT_SYSV_CURSES
13061314
Py_BEGIN_ALLOW_THREADS
13071315
rtn2 = wmove(self->win,y,x)==ERR ? ERR :

0 commit comments

Comments
 (0)