Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ef9efbd

Browse files
committed
Fix #9324: Add parameter validation to signal.signal on Windows in order
to prevent crashes.
1 parent e6fc740 commit ef9efbd

4 files changed

Lines changed: 50 additions & 5 deletions

File tree

Doc/library/signal.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ The :mod:`signal` module defines the following functions:
230230
see the :ref:`description in the type hierarchy <frame-objects>` or see the
231231
attribute descriptions in the :mod:`inspect` module).
232232

233+
On Windows, :func:`signal` can only be called with :const:`SIGABRT`,
234+
:const:`SIGFPE`, :const:`SIGILL`, :const:`SIGINT`, :const:`SIGSEGV`, or
235+
:const:`SIGTERM`. A :exc:`ValueError` will be raised in any other case.
236+
233237

234238
.. _signal-example:
235239

Lib/test/test_signal.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import traceback
1010
import sys, os, time, errno
1111

12-
if sys.platform[:3] in ('win', 'os2') or sys.platform == 'riscos':
12+
if sys.platform == 'os2' or sys.platform == 'riscos':
1313
raise unittest.SkipTest("Can't test signal on %s" % \
1414
sys.platform)
1515

@@ -37,6 +37,7 @@ def ignoring_eintr(__func, *args, **kwargs):
3737
return None
3838

3939

40+
@unittest.skipIf(sys.platform == "win32", "Not valid on Windows")
4041
class InterProcessSignalTests(unittest.TestCase):
4142
MAX_DURATION = 20 # Entire test should last at most 20 sec.
4243

@@ -186,6 +187,7 @@ def test_main(self):
186187
self.MAX_DURATION)
187188

188189

190+
@unittest.skipIf(sys.platform == "win32", "Not valid on Windows")
189191
class BasicSignalTests(unittest.TestCase):
190192
def trivial_signal_handler(self, *args):
191193
pass
@@ -208,6 +210,23 @@ def test_getsignal(self):
208210
self.assertEquals(signal.getsignal(signal.SIGHUP), hup)
209211

210212

213+
@unittest.skipUnless(sys.platform == "win32", "Windows specific")
214+
class WindowsSignalTests(unittest.TestCase):
215+
def test_issue9324(self):
216+
handler = lambda x, y: None
217+
signal.signal(signal.SIGABRT, handler)
218+
signal.signal(signal.SIGFPE, handler)
219+
signal.signal(signal.SIGILL, handler)
220+
signal.signal(signal.SIGINT, handler)
221+
signal.signal(signal.SIGSEGV, handler)
222+
signal.signal(signal.SIGTERM, handler)
223+
224+
with self.assertRaises(ValueError):
225+
signal.signal(-1, handler)
226+
sinal.signal(7, handler)
227+
228+
229+
@unittest.skipIf(sys.platform == "win32", "Not valid on Windows")
211230
class WakeupSignalTests(unittest.TestCase):
212231
TIMEOUT_FULL = 10
213232
TIMEOUT_HALF = 5
@@ -253,14 +272,15 @@ def tearDown(self):
253272
os.close(self.write)
254273
signal.signal(signal.SIGALRM, self.alrm)
255274

275+
@unittest.skipIf(sys.platform == "win32", "Not valid on Windows")
256276
class SiginterruptTest(unittest.TestCase):
257-
signum = signal.SIGUSR1
258277

259278
def setUp(self):
260279
"""Install a no-op signal handler that can be set to allow
261280
interrupts or not, and arrange for the original signal handler to be
262281
re-installed when the test is finished.
263282
"""
283+
self.signum = signal.SIGUSR1
264284
oldhandler = signal.signal(self.signum, lambda x,y: None)
265285
self.addCleanup(signal.signal, self.signum, oldhandler)
266286

@@ -354,7 +374,7 @@ def test_siginterrupt_off(self):
354374
self.assertFalse(i)
355375

356376

357-
377+
@unittest.skipIf(sys.platform == "win32", "Not valid on Windows")
358378
class ItimerTest(unittest.TestCase):
359379
def setUp(self):
360380
self.hndl_called = False
@@ -463,8 +483,11 @@ def test_itimer_prof(self):
463483
self.assertEqual(self.hndl_called, True)
464484

465485
def test_main():
466-
support.run_unittest(BasicSignalTests, InterProcessSignalTests,
467-
WakeupSignalTests, SiginterruptTest, ItimerTest)
486+
if sys.platform == "win32":
487+
support.run_unittest(WindowsSignalTests)
488+
else:
489+
support.run_unittest(BasicSignalTests, InterProcessSignalTests,
490+
WakeupSignalTests, SiginterruptTest, ItimerTest)
468491

469492

470493
if __name__ == "__main__":

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ Core and Builtins
2424
Extensions
2525
----------
2626

27+
- Issue #9324: Add parameter validation to signal.signal on Windows in order
28+
to prevent crashes.
29+
2730
- Issue #9526: Remove some outdated (int) casts that were preventing
2831
the array module from working correctly with arrays of more than
2932
2**31 elements.

Modules/signalmodule.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,23 @@ signal_signal(PyObject *self, PyObject *args)
255255
int sig_num;
256256
PyObject *old_handler;
257257
void (*func)(int);
258+
#ifdef MS_WINDOWS
259+
int cur_sig, num_valid_sigs = 6;
260+
static int valid_sigs[] = {SIGABRT, SIGFPE, SIGILL, SIGINT,
261+
SIGSEGV, SIGTERM};
262+
BOOL valid_sig = FALSE;
263+
#endif
258264
if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
259265
return NULL;
266+
#ifdef MS_WINDOWS
267+
/* Validate that sig_num is one of the allowable signals */
268+
for (cur_sig = 0; cur_sig < num_valid_sigs; cur_sig++)
269+
valid_sig |= (sig_num == valid_sigs[cur_sig]);
270+
if (!valid_sig) {
271+
PyErr_SetString(PyExc_ValueError, "signal number out of range");
272+
return NULL;
273+
}
274+
#endif
260275
#ifdef WITH_THREAD
261276
if (PyThread_get_thread_ident() != main_thread) {
262277
PyErr_SetString(PyExc_ValueError,

0 commit comments

Comments
 (0)