Skip to content

bpo-23325: Fix SIG_IGN and SIG_DFL int comparison in signal module #31759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The :mod:`signal` module no longer assumes that :const:`~signal.SIG_IGN` and
:const:`~signal.SIG_DFL` are small int singletons.
44 changes: 35 additions & 9 deletions Modules/signalmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
_signal_module_state *modstate = get_signal_state(module);
PyObject *old_handler;
void (*func)(int);
int match = 0; // cannot use func == NULL as sentinel, SIG_DFL == 0
#ifdef MS_WINDOWS
/* Validate that signalnum is one of the allowable signals */
switch (signalnum) {
Expand Down Expand Up @@ -528,21 +529,38 @@ signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
"signal number out of range");
return NULL;
}
if (handler == modstate->ignore_handler) {
func = SIG_IGN;
if (PyCallable_Check(handler)) {
func = signal_handler;
match = 1;
}
else if (handler == modstate->default_handler) {
func = SIG_DFL;
if (!match) {
int cmp = PyObject_RichCompareBool(handler, modstate->ignore_handler, Py_EQ);
switch (cmp) {
case -1:
return NULL;
case 1:
func = SIG_IGN;
match = 1;
break;
}
}
else if (!PyCallable_Check(handler)) {
if (!match) {
int cmp = PyObject_RichCompareBool(handler, modstate->default_handler, Py_EQ);
switch (cmp) {
case -1:
return NULL;
case 1:
func = SIG_DFL;
match = 1;
break;
}
}
if (!match) {
_PyErr_SetString(tstate, PyExc_TypeError,
"signal handler must be signal.SIG_IGN, "
"signal.SIG_DFL, or a callable object");
return NULL;
}
else {
func = signal_handler;
}

/* Check for pending signals before changing signal handler */
if (_PyErr_CheckSignalsTstate(tstate)) {
Expand Down Expand Up @@ -1893,7 +1911,15 @@ PyErr_SetInterruptEx(int signum)

signal_state_t *state = &signal_global_state;
PyObject *func = get_handler(signum);
if (func != state->ignore_handler && func != state->default_handler) {
int is_ign = PyObject_RichCompareBool(func, state->ignore_handler, Py_EQ);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it okay to call a Python code in PyErr_SetInterruptEx()?

If func is a custom callable, it can have an __eq__ method which can call an arbitrary Python code here.

It may be safer to use PyLong_Check() + PyLong_GetLongAndOverflow().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point!

I took a different approach and added a helper function. We can safely assume that the internal handler objects are always exact longs. Comparison between exact longs should never fail.

if (is_ign == -1) {
return -1;
}
int is_dfl = PyObject_RichCompareBool(func, state->default_handler, Py_EQ);
if (is_dfl == -1) {
return -1;
}
if ((is_ign == 0) && (is_dfl == 0)) {
trip_signal(signum);
}
return 0;
Expand Down