]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
do not allow reading negative values with getstr()
authorBenjamin Peterson <benjamin@python.org>
Sun, 14 Aug 2016 01:15:28 +0000 (18:15 -0700)
committerBenjamin Peterson <benjamin@python.org>
Sun, 14 Aug 2016 01:15:28 +0000 (18:15 -0700)
Lib/test/test_curses.py
Misc/NEWS
Modules/_cursesmodule.c

index bd7d4fca8bee8f8fd2528b19f887832d9db985bb..f049c29c6b3d6c2616ab5943b08d0d395e09dcd3 100644 (file)
@@ -163,6 +163,9 @@ class TestCurses(unittest.TestCase):
         if hasattr(curses, 'enclose'):
             stdscr.enclose()
 
+        self.assertRaises(ValueError, stdscr.getstr, -400)
+        self.assertRaises(ValueError, stdscr.getstr, 2, 3, -400)
+
 
     def test_module_funcs(self):
         "Test module-level functions"
index 2611c09625828965f141b03a3735e9ba46f795ec..ca80c73b35106ca22a17f8fe796df08a85efb90e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -13,6 +13,9 @@ Core and Builtins
 Library
 -------
 
+- In the curses module, raise an error if window.getstr() is passed a negative
+  value.
+
 - Issue #27758: Fix possible integer overflow in the _csv module for large record
   lengths.
 
index 5ffce2f9541a2d0f8ad325ed468997d35b5251c3..a8735f237a88126b23c81506bfd862e21ea1cd9c 100644 (file)
@@ -1284,6 +1284,10 @@ PyCursesWindow_GetStr(PyCursesWindowObject *self, PyObject *args)
     case 1:
         if (!PyArg_ParseTuple(args,"i;n", &n))
             return NULL;
+        if (n < 0) {
+            PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
+            return NULL;
+        }
         Py_BEGIN_ALLOW_THREADS
         rtn2 = wgetnstr(self->win, rtn, Py_MIN(n, 1023));
         Py_END_ALLOW_THREADS
@@ -1302,6 +1306,10 @@ PyCursesWindow_GetStr(PyCursesWindowObject *self, PyObject *args)
     case 3:
         if (!PyArg_ParseTuple(args,"iii;y,x,n", &y, &x, &n))
             return NULL;
+        if (n < 0) {
+            PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
+            return NULL;
+        }
 #ifdef STRICT_SYSV_CURSES
         Py_BEGIN_ALLOW_THREADS
         rtn2 = wmove(self->win,y,x)==ERR ? ERR :