Skip to content

Commit c2ba931

Browse files
gh-120378: Fix crash caused by integer overflow in curses (#124555)
This is actually an upstream problem in curses, and has been reported to them already: https://lists.gnu.org/archive/html/bug-ncurses/2024-09/msg00101.html This is a nice workaround in the meantime to prevent the segfault. Co-authored-by: Bénédikt Tran <[email protected]>
1 parent 7bd9dbf commit c2ba931

File tree

4 files changed

+105
-27
lines changed

4 files changed

+105
-27
lines changed

Lib/test/test_curses.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,14 @@ def test_resize_term(self):
10811081
self.assertEqual(curses.LINES, lines)
10821082
self.assertEqual(curses.COLS, cols)
10831083

1084+
with self.assertRaises(OverflowError):
1085+
curses.resize_term(35000, 1)
1086+
with self.assertRaises(OverflowError):
1087+
curses.resize_term(1, 35000)
1088+
# GH-120378: Overflow failure in resize_term() causes refresh to fail
1089+
tmp = curses.initscr()
1090+
tmp.erase()
1091+
10841092
@requires_curses_func('resizeterm')
10851093
def test_resizeterm(self):
10861094
curses.update_lines_cols()
@@ -1095,6 +1103,14 @@ def test_resizeterm(self):
10951103
self.assertEqual(curses.LINES, lines)
10961104
self.assertEqual(curses.COLS, cols)
10971105

1106+
with self.assertRaises(OverflowError):
1107+
curses.resizeterm(35000, 1)
1108+
with self.assertRaises(OverflowError):
1109+
curses.resizeterm(1, 35000)
1110+
# GH-120378: Overflow failure in resizeterm() causes refresh to fail
1111+
tmp = curses.initscr()
1112+
tmp.erase()
1113+
10981114
def test_ungetch(self):
10991115
curses.ungetch(b'A')
11001116
self.assertEqual(self.stdscr.getkey(), 'A')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a crash related to an integer overflow in :func:`curses.resizeterm`
2+
and :func:`curses.resize_term`.

Modules/_cursesmodule.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4204,9 +4204,9 @@ NoArgNoReturnFunctionBody(resetty)
42044204
/*[clinic input]
42054205
_curses.resizeterm
42064206
4207-
nlines: int
4207+
nlines: short
42084208
Height.
4209-
ncols: int
4209+
ncols: short
42104210
Width.
42114211
/
42124212
@@ -4217,8 +4217,8 @@ window dimensions (in particular the SIGWINCH handler).
42174217
[clinic start generated code]*/
42184218

42194219
static PyObject *
4220-
_curses_resizeterm_impl(PyObject *module, int nlines, int ncols)
4221-
/*[clinic end generated code: output=56d6bcc5194ad055 input=0fca02ebad5ffa82]*/
4220+
_curses_resizeterm_impl(PyObject *module, short nlines, short ncols)
4221+
/*[clinic end generated code: output=4de3abab50c67f02 input=414e92a63e3e9899]*/
42224222
{
42234223
PyObject *result;
42244224

@@ -4240,9 +4240,9 @@ _curses_resizeterm_impl(PyObject *module, int nlines, int ncols)
42404240
/*[clinic input]
42414241
_curses.resize_term
42424242
4243-
nlines: int
4243+
nlines: short
42444244
Height.
4245-
ncols: int
4245+
ncols: short
42464246
Width.
42474247
/
42484248
@@ -4256,8 +4256,8 @@ without additional interaction with the application.
42564256
[clinic start generated code]*/
42574257

42584258
static PyObject *
4259-
_curses_resize_term_impl(PyObject *module, int nlines, int ncols)
4260-
/*[clinic end generated code: output=9e26d8b9ea311ed2 input=2197edd05b049ed4]*/
4259+
_curses_resize_term_impl(PyObject *module, short nlines, short ncols)
4260+
/*[clinic end generated code: output=46c6d749fa291dbd input=276afa43d8ea7091]*/
42614261
{
42624262
PyObject *result;
42634263

Modules/clinic/_cursesmodule.c.h

Lines changed: 79 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)