diff --git a/Lib/test/test_curses.py b/Lib/test/test_curses.py index 082763aa3f0611b..02d7b5bd8f2cc9b 100644 --- a/Lib/test/test_curses.py +++ b/Lib/test/test_curses.py @@ -1014,6 +1014,13 @@ def test_terminfo(self): self.assertIsInstance(cud_2, bytes) curses.putp(cud_2) + # String parameters are not supported. An omitted one is empty. + ms = curses.tigetstr('Ms') + if ms is not None and b'%p2%s' in ms: + self.assertRaises(TypeError, curses.tparm, ms, 1, 2) + self.assertRaises(TypeError, curses.tparm, ms, 0, 2) + self.assertNotIn(b'%', curses.tparm(ms)) + curses.putp(b'abc\n') def test_misc_module_funcs(self): diff --git a/Misc/NEWS.d/next/Library/2026-11-07-20-26-52.gh-issue-161571.qQ8OhA.rst b/Misc/NEWS.d/next/Library/2026-11-07-20-26-52.gh-issue-161571.qQ8OhA.rst new file mode 100644 index 000000000000000..c24b85994156330 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-07-20-26-52.gh-issue-161571.qQ8OhA.rst @@ -0,0 +1,3 @@ +Fix a crash in :func:`curses.tparm` for capabilities with parameters +that they format as strings, such as ``Ms`` of xterm. +Non-zero integers for these parameters now raise :exc:`TypeError`. diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index ab1934d364c6ecb..eab04a5bd144f34 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -5069,6 +5069,66 @@ _curses_tigetstr_impl(PyObject *module, const char *capname) return PyBytes_FromString( capname ); } +/* Return a bit mask of the parameters of a terminfo string that curses takes + as strings: those that %s or %l follows directly after %p, as in ncurses' + _nc_tparm_analyze(). The others are numbers. */ +static unsigned int +curses_tparm_string_params(const char *str) +{ + unsigned int mask = 0; + int last = 0; /* the parameter pushed last, or 0 */ + + while (*str) { + if (*str++ != '%') { + continue; + } + /* Skip the flags, width and precision of a conversion. */ + int allowminus = 0; + while (*str) { + if (*str == ':') { + allowminus = 1; + } + else if (!(*str == '#' || *str == ' ' || *str == '.' + || Py_ISDIGIT(*str) || (*str == '-' && allowminus))) + { + break; + } + str++; + } + switch (*str) { + case 'p': + if (str[1] >= '1' && str[1] <= '9') { + last = str[1] - '0'; + } + str++; + break; + case 's': + case 'l': + if (last > 0) { + mask |= 1u << (last - 1); + } + break; + case 'd': case 'o': case 'x': case 'X': case 'c': + case '+': case '-': case '*': case '/': case 'm': + case '&': case '|': case '^': case '=': case '<': case '>': + case 'A': case 'O': case '!': case '~': + last = 0; + break; + case '\'': + str += 2; + last = 0; + break; + case '\0': + return mask; + default: + /* %P, %g, %{, %i, %?, %t, %e, %; and %% keep it. */ + break; + } + str++; + } + return mask; +} + /*[clinic input] _curses.tparm @@ -5097,6 +5157,18 @@ _curses_tparm_impl(PyObject *module, const char *str, int i1, int i2, int i3, PyCursesStatefulSetupTermCalled(module); + /* curses would take a non-zero number as a pointer to a string. */ + unsigned int strings = curses_tparm_string_params(str); + int params[9] = {i1, i2, i3, i4, i5, i6, i7, i8, i9}; + for (int i = 0; i < 9; i++) { + if ((strings & (1u << i)) && params[i] != 0) { + PyErr_Format(PyExc_TypeError, + "tparm() argument %d: string parameters " + "are not supported", i + 2); + return NULL; + } + } + result = tparm((char *)str,i1,i2,i3,i4,i5,i6,i7,i8,i9); if (!result) { curses_set_null_error(module, "tparm", NULL);