Skip to content

Commit 2dde49a

Browse files
gh-161571: Fix a crash in curses.tparm() with string parameters (GH-161601)
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. An integer there crashed it. Such parameters must now be bytes objects, which are passed as char *. The parameters are now a var-positional parameter. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ebebe05 commit 2dde49a

5 files changed

Lines changed: 175 additions & 34 deletions

File tree

‎Doc/library/curses.rst‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1032,10 +1032,15 @@ Terminfo database
10321032
Instantiate the bytes object *str* with the supplied parameters, where *str* should
10331033
be a parameterized byte string obtained from the terminfo database. For example,
10341034
``tparm(tigetstr("cup"), 5, 3)`` could result in ``b'\033[6;4H'``, the exact
1035-
result depending on terminal type. Up to nine integer parameters may be supplied.
1035+
result depending on terminal type. Up to nine parameters may be supplied.
1036+
They are integers, except in a few cases, such as ``Ms`` of xterm,
1037+
where they must be bytes objects.
10361038

10371039
:func:`setupterm` (or :func:`initscr`) must be called first.
10381040

1041+
.. versionchanged:: next
1042+
Added support of bytes parameters.
1043+
10391044
.. function:: putp(str)
10401045

10411046
Equivalent to ``tputs(str, 1, putchar)``; emit the value of a specified

‎Lib/test/test_curses.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,6 +1873,17 @@ def test_terminfo(self):
18731873
self.assertIsInstance(cud_2, bytes)
18741874
curses.putp(cud_2)
18751875

1876+
# The few capabilities that take strings take them as bytes.
1877+
ms = curses.tigetstr('Ms')
1878+
if ms is not None and b'%p2%s' in ms:
1879+
self.assertIn(b'aGk=', curses.tparm(ms, b'c', b'aGk='))
1880+
self.assertRaises(TypeError, curses.tparm, ms, 1, 2)
1881+
self.assertRaises(ValueError, curses.tparm, ms, b'c', b'a\0')
1882+
# An omitted string is empty.
1883+
self.assertNotIn(b'%', curses.tparm(ms, b'c'))
1884+
self.assertRaises(TypeError, curses.tparm, b'%p1%d', b'x')
1885+
self.assertRaises(TypeError, curses.tparm, b'%p1%d', *range(10))
1886+
18761887
curses.putp(b'abc\n')
18771888

18781889
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+
Bytes objects are now accepted for these parameters.

‎Modules/_cursesmodule.c‎

Lines changed: 134 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8374,35 +8374,155 @@ _curses_tigetstr_impl(PyObject *module, const char *capname)
83748374
return PyBytes_FromString( capname );
83758375
}
83768376

8377+
#ifndef NCURSES_TPARM_ARG
8378+
# define NCURSES_TPARM_ARG long
8379+
#endif
8380+
8381+
/* Return a bit mask of the parameters of a terminfo string that curses takes
8382+
as strings: those that %s or %l follows directly after %p, as in ncurses'
8383+
_nc_tparm_analyze(). The others are numbers. */
8384+
static unsigned int
8385+
curses_tparm_string_params(const char *str)
8386+
{
8387+
unsigned int mask = 0;
8388+
int last = 0; /* the parameter pushed last, or 0 */
8389+
8390+
while (*str) {
8391+
if (*str++ != '%') {
8392+
continue;
8393+
}
8394+
/* Skip the flags, width and precision of a conversion. */
8395+
int allowminus = 0;
8396+
while (*str) {
8397+
if (*str == ':') {
8398+
allowminus = 1;
8399+
}
8400+
else if (!(*str == '#' || *str == ' ' || *str == '.'
8401+
|| Py_ISDIGIT(*str) || (*str == '-' && allowminus)))
8402+
{
8403+
break;
8404+
}
8405+
str++;
8406+
}
8407+
switch (*str) {
8408+
case 'p':
8409+
if (str[1] >= '1' && str[1] <= '9') {
8410+
last = str[1] - '0';
8411+
}
8412+
str++;
8413+
break;
8414+
case 's':
8415+
case 'l':
8416+
if (last > 0) {
8417+
mask |= 1u << (last - 1);
8418+
}
8419+
break;
8420+
case 'd': case 'o': case 'x': case 'X': case 'c':
8421+
case '+': case '-': case '*': case '/': case 'm':
8422+
case '&': case '|': case '^': case '=': case '<': case '>':
8423+
case 'A': case 'O': case '!': case '~':
8424+
last = 0;
8425+
break;
8426+
case '\'':
8427+
str += 2;
8428+
last = 0;
8429+
break;
8430+
case '\0':
8431+
return mask;
8432+
default:
8433+
/* %P, %g, %{, %i, %?, %t, %e, %; and %% keep it. */
8434+
break;
8435+
}
8436+
str++;
8437+
}
8438+
return mask;
8439+
}
8440+
8441+
/* Convert a tparm() parameter: a string for those that the terminfo string
8442+
takes as strings (passing a number there would crash curses), otherwise a
8443+
number. An omitted one is 0, or NULL, which curses takes as "". */
8444+
static int
8445+
curses_tparm_param(PyObject *arg, int argnum, int is_string,
8446+
NCURSES_TPARM_ARG *result)
8447+
{
8448+
if (arg == NULL) {
8449+
*result = 0;
8450+
return 0;
8451+
}
8452+
if (is_string) {
8453+
if (!PyBytes_Check(arg)) {
8454+
PyErr_Format(PyExc_TypeError,
8455+
"tparm() argument %d must be bytes, not %T",
8456+
argnum, arg);
8457+
return -1;
8458+
}
8459+
const char *value = PyBytes_AS_STRING(arg);
8460+
if (strlen(value) != (size_t)PyBytes_GET_SIZE(arg)) {
8461+
PyErr_SetString(PyExc_ValueError, "embedded null byte");
8462+
return -1;
8463+
}
8464+
*result = (NCURSES_TPARM_ARG)(intptr_t)value;
8465+
return 0;
8466+
}
8467+
if (PyBytes_Check(arg)) {
8468+
PyErr_Format(PyExc_TypeError,
8469+
"tparm() argument %d must be int, not bytes", argnum);
8470+
return -1;
8471+
}
8472+
int value = PyLong_AsInt(arg);
8473+
if (value == -1 && PyErr_Occurred()) {
8474+
return -1;
8475+
}
8476+
*result = value;
8477+
return 0;
8478+
}
8479+
83778480
/*[clinic input]
83788481
_curses.tparm
83798482
8380-
str: str(accept={robuffer})
8483+
str as string: object
83818484
Parameterized byte string obtained from the terminfo database.
8382-
i1: int = 0
8383-
i2: int = 0
8384-
i3: int = 0
8385-
i4: int = 0
8386-
i5: int = 0
8387-
i6: int = 0
8388-
i7: int = 0
8389-
i8: int = 0
8390-
i9: int = 0
83918485
/
8486+
*params: tuple
8487+
Up to nine parameters.
83928488
83938489
Instantiate the specified byte string with the supplied parameters.
8490+
8491+
The parameters are integers, except in a few cases where they must be
8492+
bytes objects.
83948493
[clinic start generated code]*/
83958494

83968495
static PyObject *
8397-
_curses_tparm_impl(PyObject *module, const char *str, int i1, int i2, int i3,
8398-
int i4, int i5, int i6, int i7, int i8, int i9)
8399-
/*[clinic end generated code: output=599f62b615c667ff input=5e30b15786f032aa]*/
8496+
_curses_tparm_impl(PyObject *module, PyObject *string, PyObject *params)
8497+
/*[clinic end generated code: output=3439bb1b37a13d5b input=4889c0e3a5eab5b5]*/
84008498
{
84018499
char* result = NULL;
8500+
NCURSES_TPARM_ARG values[9];
8501+
Py_ssize_t count = PyTuple_GET_SIZE(params);
8502+
const char *str;
84028503

8504+
if (!PyArg_Parse(string, "y:tparm", &str)) {
8505+
return NULL;
8506+
}
8507+
8508+
if (count > 9) {
8509+
PyErr_Format(PyExc_TypeError,
8510+
"tparm() takes at most 9 parameters (%zd given)", count);
8511+
return NULL;
8512+
}
84038513
PyCursesStatefulSetupTermCalled(module);
84048514

8405-
result = tparm((char *)str,i1,i2,i3,i4,i5,i6,i7,i8,i9);
8515+
unsigned int strings = curses_tparm_string_params(str);
8516+
for (int i = 0; i < 9; i++) {
8517+
PyObject *arg = i < count ? PyTuple_GET_ITEM(params, i) : NULL;
8518+
if (curses_tparm_param(arg, i + 2, strings & (1u << i),
8519+
&values[i]) < 0)
8520+
{
8521+
return NULL;
8522+
}
8523+
}
8524+
result = tparm((char *)str, values[0], values[1], values[2], values[3],
8525+
values[4], values[5], values[6], values[7], values[8]);
84068526
if (!result) {
84078527
curses_set_null_error(module, "tparm", NULL);
84088528
return NULL;

‎Modules/clinic/_cursesmodule.c.h‎

Lines changed: 21 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)