Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Lib/test/test_curses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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`.
72 changes: 72 additions & 0 deletions Modules/_cursesmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -4760,6 +4760,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

Expand Down Expand Up @@ -4788,6 +4848,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) {
cursesmodule_state *state = get_cursesmodule_state(module);
Expand Down
Loading