Skip to content

Commit 08c45ff

Browse files
[3.15] gh-161571: Fix a crash in curses.tparm() with string parameters (GH-161601) (GH-162558)
curses passes the parameters that the format string formats with %s or %l after %p as char *, for example in the Ms capability of xterm. A non-zero integer there crashed it. It now raises TypeError. (cherry picked from commit 2dde49a) Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 326f1c1 commit 08c45ff

3 files changed

Lines changed: 82 additions & 0 deletions

File tree

‎Lib/test/test_curses.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,13 @@ def test_terminfo(self):
10141014
self.assertIsInstance(cud_2, bytes)
10151015
curses.putp(cud_2)
10161016

1017+
# String parameters are not supported. An omitted one is empty.
1018+
ms = curses.tigetstr('Ms')
1019+
if ms is not None and b'%p2%s' in ms:
1020+
self.assertRaises(TypeError, curses.tparm, ms, 1, 2)
1021+
self.assertRaises(TypeError, curses.tparm, ms, 0, 2)
1022+
self.assertNotIn(b'%', curses.tparm(ms))
1023+
10171024
curses.putp(b'abc\n')
10181025

10191026
def test_misc_module_funcs(self):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a crash in :func:`curses.tparm` for capabilities with parameters
2+
that they format as strings, such as ``Ms`` of xterm.
3+
Non-zero integers for these parameters now raise :exc:`TypeError`.

‎Modules/_cursesmodule.c‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5069,6 +5069,66 @@ _curses_tigetstr_impl(PyObject *module, const char *capname)
50695069
return PyBytes_FromString( capname );
50705070
}
50715071

5072+
/* Return a bit mask of the parameters of a terminfo string that curses takes
5073+
as strings: those that %s or %l follows directly after %p, as in ncurses'
5074+
_nc_tparm_analyze(). The others are numbers. */
5075+
static unsigned int
5076+
curses_tparm_string_params(const char *str)
5077+
{
5078+
unsigned int mask = 0;
5079+
int last = 0; /* the parameter pushed last, or 0 */
5080+
5081+
while (*str) {
5082+
if (*str++ != '%') {
5083+
continue;
5084+
}
5085+
/* Skip the flags, width and precision of a conversion. */
5086+
int allowminus = 0;
5087+
while (*str) {
5088+
if (*str == ':') {
5089+
allowminus = 1;
5090+
}
5091+
else if (!(*str == '#' || *str == ' ' || *str == '.'
5092+
|| Py_ISDIGIT(*str) || (*str == '-' && allowminus)))
5093+
{
5094+
break;
5095+
}
5096+
str++;
5097+
}
5098+
switch (*str) {
5099+
case 'p':
5100+
if (str[1] >= '1' && str[1] <= '9') {
5101+
last = str[1] - '0';
5102+
}
5103+
str++;
5104+
break;
5105+
case 's':
5106+
case 'l':
5107+
if (last > 0) {
5108+
mask |= 1u << (last - 1);
5109+
}
5110+
break;
5111+
case 'd': case 'o': case 'x': case 'X': case 'c':
5112+
case '+': case '-': case '*': case '/': case 'm':
5113+
case '&': case '|': case '^': case '=': case '<': case '>':
5114+
case 'A': case 'O': case '!': case '~':
5115+
last = 0;
5116+
break;
5117+
case '\'':
5118+
str += 2;
5119+
last = 0;
5120+
break;
5121+
case '\0':
5122+
return mask;
5123+
default:
5124+
/* %P, %g, %{, %i, %?, %t, %e, %; and %% keep it. */
5125+
break;
5126+
}
5127+
str++;
5128+
}
5129+
return mask;
5130+
}
5131+
50725132
/*[clinic input]
50735133
_curses.tparm
50745134
@@ -5097,6 +5157,18 @@ _curses_tparm_impl(PyObject *module, const char *str, int i1, int i2, int i3,
50975157

50985158
PyCursesStatefulSetupTermCalled(module);
50995159

5160+
/* curses would take a non-zero number as a pointer to a string. */
5161+
unsigned int strings = curses_tparm_string_params(str);
5162+
int params[9] = {i1, i2, i3, i4, i5, i6, i7, i8, i9};
5163+
for (int i = 0; i < 9; i++) {
5164+
if ((strings & (1u << i)) && params[i] != 0) {
5165+
PyErr_Format(PyExc_TypeError,
5166+
"tparm() argument %d: string parameters "
5167+
"are not supported", i + 2);
5168+
return NULL;
5169+
}
5170+
}
5171+
51005172
result = tparm((char *)str,i1,i2,i3,i4,i5,i6,i7,i8,i9);
51015173
if (!result) {
51025174
curses_set_null_error(module, "tparm", NULL);

0 commit comments

Comments
 (0)