Skip to content

Commit 432ea4f

Browse files
committed
fail when negative values are passed to instr()
1 parent 5e4aafa commit 432ea4f

3 files changed

Lines changed: 12 additions & 2 deletions

File tree

‎Lib/test/test_curses.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ def test_window_funcs(self):
165165

166166
self.assertRaises(ValueError, stdscr.getstr, -400)
167167
self.assertRaises(ValueError, stdscr.getstr, 2, 3, -400)
168+
self.assertRaises(ValueError, stdscr.instr, -2)
169+
self.assertRaises(ValueError, stdscr.instr, 2, 3, -2)
168170

169171

170172
def test_module_funcs(self):

‎Misc/NEWS‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ Core and Builtins
1313
Library
1414
-------
1515

16-
- In the curses module, raise an error if window.getstr() is passed a negative
17-
value.
16+
- In the curses module, raise an error if window.getstr() or window.instr() is
17+
passed a negative value.
1818

1919
- Issue #27760: Fix possible integer overflow in binascii.b2a_qp.
2020

‎Modules/_cursesmodule.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,10 @@ PyCursesWindow_InStr(PyCursesWindowObject *self, PyObject *args)
14561456
case 1:
14571457
if (!PyArg_ParseTuple(args,"i;n", &n))
14581458
return NULL;
1459+
if (n < 0) {
1460+
PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
1461+
return NULL;
1462+
}
14591463
rtn2 = winnstr(self->win, rtn, Py_MIN(n, 1023));
14601464
break;
14611465
case 2:
@@ -1466,6 +1470,10 @@ PyCursesWindow_InStr(PyCursesWindowObject *self, PyObject *args)
14661470
case 3:
14671471
if (!PyArg_ParseTuple(args, "iii;y,x,n", &y, &x, &n))
14681472
return NULL;
1473+
if (n < 0) {
1474+
PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
1475+
return NULL;
1476+
}
14691477
rtn2 = mvwinnstr(self->win, y, x, rtn, Py_MIN(n,1023));
14701478
break;
14711479
default:

0 commit comments

Comments
 (0)