summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2021-01-03 22:55:23 (GMT)
committerGitHub <noreply@github.com>2021-01-03 22:55:23 (GMT)
commitb0ee2b492dbf550fbd2a63b82de0a4dc9d67f32e (patch)
tree90becc4d4e3a8851a387a67bd0e9c7bcf4b897f9 /Modules
parent0303008ebceb6ac6035cd9722d1393267304171d (diff)
downloadcpython-b0ee2b492dbf550fbd2a63b82de0a4dc9d67f32e.zip
cpython-b0ee2b492dbf550fbd2a63b82de0a4dc9d67f32e.tar.gz
cpython-b0ee2b492dbf550fbd2a63b82de0a4dc9d67f32e.tar.bz2
[3.9] bpo-42681: Fix range checks for color and pair numbers in curses (GH-23874). (GH-24077)
(cherry picked from commit 1470edd6131c29b8a09ce012cdfee3afa269d553)
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_cursesmodule.c44
-rw-r--r--Modules/clinic/_cursesmodule.c.h26
2 files changed, 29 insertions, 41 deletions
diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c
index c70b0e2..0914e20 100644
--- a/Modules/_cursesmodule.c
+++ b/Modules/_cursesmodule.c
@@ -176,18 +176,6 @@ static char *screen_encoding = NULL;
/* Utility Functions */
-static inline int
-color_pair_to_attr(short color_number)
-{
- return ((int)color_number << 8);
-}
-
-static inline short
-attr_to_color_pair(int attr)
-{
- return (short)((attr & A_COLOR) >> 8);
-}
-
/*
* Check the return code from a curses function and return None
* or raise an exception as appropriate. These are exported using the
@@ -618,7 +606,7 @@ _curses_window_addch_impl(PyCursesWindowObject *self, int group_left_1,
if (type == 2) {
funcname = "add_wch";
wstr[1] = L'\0';
- setcchar(&wcval, wstr, attr, attr_to_color_pair(attr), NULL);
+ setcchar(&wcval, wstr, attr, PAIR_NUMBER(attr), NULL);
if (coordinates_group)
rtn = mvwadd_wch(self->win,y,x, &wcval);
else {
@@ -2586,7 +2574,7 @@ NoArgOrFlagNoReturnFunctionBody(cbreak, flag)
_curses.color_content
color_number: short
- The number of the color (0 - COLORS).
+ The number of the color (0 - (COLORS-1)).
/
Return the red, green, and blue (RGB) components of the specified color.
@@ -2597,7 +2585,7 @@ which will be between 0 (no component) and 1000 (maximum amount of component).
static PyObject *
_curses_color_content_impl(PyObject *module, short color_number)
-/*[clinic end generated code: output=cb15cf3120d4bfc1 input=5555abb1c11e11b7]*/
+/*[clinic end generated code: output=cb15cf3120d4bfc1 input=630f6737514db6ad]*/
{
short r,g,b;
@@ -2616,8 +2604,8 @@ _curses_color_content_impl(PyObject *module, short color_number)
/*[clinic input]
_curses.color_pair
- color_number: short
- The number of the color (0 - COLORS).
+ pair_number: short
+ The number of the color pair.
/
Return the attribute value for displaying text in the specified color.
@@ -2627,13 +2615,13 @@ other A_* attributes. pair_number() is the counterpart to this function.
[clinic start generated code]*/
static PyObject *
-_curses_color_pair_impl(PyObject *module, short color_number)
-/*[clinic end generated code: output=6a84cb6b29ecaf9a input=a9d3eb6f50e4dc12]*/
+_curses_color_pair_impl(PyObject *module, short pair_number)
+/*[clinic end generated code: output=ce609d238b70dc11 input=8dd0d5da94cb15b5]*/
{
PyCursesInitialised;
PyCursesInitialisedColor;
- return PyLong_FromLong(color_pair_to_attr(color_number));
+ return PyLong_FromLong(COLOR_PAIR(pair_number));
}
/*[clinic input]
@@ -3028,7 +3016,7 @@ _curses_has_key_impl(PyObject *module, int key)
_curses.init_color
color_number: short
- The number of the color to be changed (0 - COLORS).
+ The number of the color to be changed (0 - (COLORS-1)).
r: short
Red component (0 - 1000).
g: short
@@ -3041,13 +3029,13 @@ Change the definition of a color.
When init_color() is used, all occurrences of that color on the screen
immediately change to the new definition. This function is a no-op on
-most terminals; it is active only if can_change_color() returns 1.
+most terminals; it is active only if can_change_color() returns true.
[clinic start generated code]*/
static PyObject *
_curses_init_color_impl(PyObject *module, short color_number, short r,
short g, short b)
-/*[clinic end generated code: output=280236f5efe9776a input=f3a05bd38f619175]*/
+/*[clinic end generated code: output=280236f5efe9776a input=128601b5dc76d548]*/
{
PyCursesInitialised;
PyCursesInitialisedColor;
@@ -3061,9 +3049,9 @@ _curses.init_pair
pair_number: short
The number of the color-pair to be changed (1 - (COLOR_PAIRS-1)).
fg: short
- Foreground color number (0 - COLORS).
+ Foreground color number (-1 - (COLORS-1)).
bg: short
- Background color number (0 - COLORS).
+ Background color number (-1 - (COLORS-1)).
/
Change the definition of a color-pair.
@@ -3075,7 +3063,7 @@ all occurrences of that color-pair are changed to the new definition.
static PyObject *
_curses_init_pair_impl(PyObject *module, short pair_number, short fg,
short bg)
-/*[clinic end generated code: output=9c2ce39c22f376b6 input=c9f0b11b17a2ac6d]*/
+/*[clinic end generated code: output=9c2ce39c22f376b6 input=12c320ec14396ea2]*/
{
PyCursesInitialised;
PyCursesInitialisedColor;
@@ -3715,7 +3703,7 @@ _curses_pair_content_impl(PyObject *module, short pair_number)
if (pair_content(pair_number, &f, &b)==ERR) {
PyErr_SetString(PyCursesError,
- "Argument 1 was out of range. (1..COLOR_PAIRS-1)");
+ "Argument 1 was out of range. (0..COLOR_PAIRS-1)");
return NULL;
}
@@ -3740,7 +3728,7 @@ _curses_pair_number_impl(PyObject *module, int attr)
PyCursesInitialised;
PyCursesInitialisedColor;
- return PyLong_FromLong(attr_to_color_pair(attr));
+ return PyLong_FromLong(PAIR_NUMBER(attr));
}
/*[clinic input]
diff --git a/Modules/clinic/_cursesmodule.c.h b/Modules/clinic/_cursesmodule.c.h
index 50d7f21..66ffcdf 100644
--- a/Modules/clinic/_cursesmodule.c.h
+++ b/Modules/clinic/_cursesmodule.c.h
@@ -2028,7 +2028,7 @@ PyDoc_STRVAR(_curses_color_content__doc__,
"Return the red, green, and blue (RGB) components of the specified color.\n"
"\n"
" color_number\n"
-" The number of the color (0 - COLORS).\n"
+" The number of the color (0 - (COLORS-1)).\n"
"\n"
"A 3-tuple is returned, containing the R, G, B values for the given color,\n"
"which will be between 0 (no component) and 1000 (maximum amount of component).");
@@ -2076,13 +2076,13 @@ exit:
}
PyDoc_STRVAR(_curses_color_pair__doc__,
-"color_pair($module, color_number, /)\n"
+"color_pair($module, pair_number, /)\n"
"--\n"
"\n"
"Return the attribute value for displaying text in the specified color.\n"
"\n"
-" color_number\n"
-" The number of the color (0 - COLORS).\n"
+" pair_number\n"
+" The number of the color pair.\n"
"\n"
"This attribute value can be combined with A_STANDOUT, A_REVERSE, and the\n"
"other A_* attributes. pair_number() is the counterpart to this function.");
@@ -2091,13 +2091,13 @@ PyDoc_STRVAR(_curses_color_pair__doc__,
{"color_pair", (PyCFunction)_curses_color_pair, METH_O, _curses_color_pair__doc__},
static PyObject *
-_curses_color_pair_impl(PyObject *module, short color_number);
+_curses_color_pair_impl(PyObject *module, short pair_number);
static PyObject *
_curses_color_pair(PyObject *module, PyObject *arg)
{
PyObject *return_value = NULL;
- short color_number;
+ short pair_number;
if (PyFloat_Check(arg)) {
PyErr_SetString(PyExc_TypeError,
@@ -2120,10 +2120,10 @@ _curses_color_pair(PyObject *module, PyObject *arg)
goto exit;
}
else {
- color_number = (short) ival;
+ pair_number = (short) ival;
}
}
- return_value = _curses_color_pair_impl(module, color_number);
+ return_value = _curses_color_pair_impl(module, pair_number);
exit:
return return_value;
@@ -2699,7 +2699,7 @@ PyDoc_STRVAR(_curses_init_color__doc__,
"Change the definition of a color.\n"
"\n"
" color_number\n"
-" The number of the color to be changed (0 - COLORS).\n"
+" The number of the color to be changed (0 - (COLORS-1)).\n"
" r\n"
" Red component (0 - 1000).\n"
" g\n"
@@ -2709,7 +2709,7 @@ PyDoc_STRVAR(_curses_init_color__doc__,
"\n"
"When init_color() is used, all occurrences of that color on the screen\n"
"immediately change to the new definition. This function is a no-op on\n"
-"most terminals; it is active only if can_change_color() returns 1.");
+"most terminals; it is active only if can_change_color() returns true.");
#define _CURSES_INIT_COLOR_METHODDEF \
{"init_color", (PyCFunction)(void(*)(void))_curses_init_color, METH_FASTCALL, _curses_init_color__doc__},
@@ -2841,9 +2841,9 @@ PyDoc_STRVAR(_curses_init_pair__doc__,
" pair_number\n"
" The number of the color-pair to be changed (1 - (COLOR_PAIRS-1)).\n"
" fg\n"
-" Foreground color number (0 - COLORS).\n"
+" Foreground color number (-1 - (COLORS-1)).\n"
" bg\n"
-" Background color number (0 - COLORS).\n"
+" Background color number (-1 - (COLORS-1)).\n"
"\n"
"If the color-pair was previously initialized, the screen is refreshed and\n"
"all occurrences of that color-pair are changed to the new definition.");
@@ -4713,4 +4713,4 @@ _curses_use_default_colors(PyObject *module, PyObject *Py_UNUSED(ignored))
#ifndef _CURSES_USE_DEFAULT_COLORS_METHODDEF
#define _CURSES_USE_DEFAULT_COLORS_METHODDEF
#endif /* !defined(_CURSES_USE_DEFAULT_COLORS_METHODDEF) */
-/*[clinic end generated code: output=b53652f8acafd817 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=5e739120041df368 input=a9049054013a1b77]*/