Skip to content

Commit 505989c

Browse files
committed
fail when negative values are passed to instr()
1 parent b97001e commit 505989c

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
@@ -187,6 +187,8 @@ def test_window_funcs(self):
187187

188188
self.assertRaises(ValueError, stdscr.getstr, -400)
189189
self.assertRaises(ValueError, stdscr.getstr, 2, 3, -400)
190+
self.assertRaises(ValueError, stdscr.instr, -2)
191+
self.assertRaises(ValueError, stdscr.instr, 2, 3, -2)
190192

191193

192194
def test_module_funcs(self):

Misc/NEWS

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ Library
3131

3232
- Issue #27760: Fix possible integer overflow in binascii.b2a_qp.
3333

34-
- In the curses module, raise an error if window.getstr() is passed a negative
35-
value.
34+
- In the curses module, raise an error if window.getstr() or window.instr() is
35+
passed a negative value.
3636

3737
- Issue #27758: Fix possible integer overflow in the _csv module for large record
3838
lengths.

Modules/_cursesmodule.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,10 @@ PyCursesWindow_InStr(PyCursesWindowObject *self, PyObject *args)
10951095
case 1:
10961096
if (!PyArg_ParseTuple(args,"i;n", &n))
10971097
return NULL;
1098+
if (n < 0) {
1099+
PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
1100+
return NULL;
1101+
}
10981102
rtn2 = winnstr(self->win,rtn,MIN(n,1023));
10991103
break;
11001104
case 2:
@@ -1105,6 +1109,10 @@ PyCursesWindow_InStr(PyCursesWindowObject *self, PyObject *args)
11051109
case 3:
11061110
if (!PyArg_ParseTuple(args, "iii;y,x,n", &y, &x, &n))
11071111
return NULL;
1112+
if (n < 0) {
1113+
PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
1114+
return NULL;
1115+
}
11081116
rtn2 = mvwinnstr(self->win, y, x, rtn, MIN(n,1023));
11091117
break;
11101118
default:

0 commit comments

Comments
 (0)