Skip to content

Commit a92cc91

Browse files
Issue #17919: Fixed integer overflow in the eventmask parameter.
1 parent 91b88c8 commit a92cc91

3 files changed

Lines changed: 35 additions & 13 deletions

File tree

Lib/test/test_poll.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44
import random
55
import select
6-
import _testcapi
6+
from _testcapi import USHRT_MAX, INT_MAX, UINT_MAX
77
try:
88
import threading
99
except ImportError:
@@ -159,10 +159,13 @@ def test_poll3(self):
159159
if x != 5:
160160
self.fail('Overflow must have occurred')
161161

162-
pollster = select.poll()
163-
# Issue 15989
164-
self.assertRaises(OverflowError, pollster.poll, _testcapi.INT_MAX + 1)
165-
self.assertRaises(OverflowError, pollster.poll, _testcapi.UINT_MAX + 1)
162+
# Issues #15989, #17919
163+
self.assertRaises(OverflowError, pollster.register, 0, -1)
164+
self.assertRaises(OverflowError, pollster.register, 0, USHRT_MAX + 1)
165+
self.assertRaises(OverflowError, pollster.modify, 1, -1)
166+
self.assertRaises(OverflowError, pollster.modify, 1, USHRT_MAX + 1)
167+
self.assertRaises(OverflowError, pollster.poll, INT_MAX + 1)
168+
self.assertRaises(OverflowError, pollster.poll, UINT_MAX + 1)
166169

167170
@unittest.skipUnless(threading, 'Threading required for this test.')
168171
@reap_threads

Misc/NEWS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ Core and Builtins
2323
Library
2424
-------
2525

26-
- Issue #17919: select.poll.poll() again works with poll.POLLNVAL on AIX.
26+
- Issue #17919: select.poll.register() again works with poll.POLLNVAL on AIX.
27+
Fixed integer overflow in the eventmask parameter.
2728

2829
- Issue #17200: telnetlib's read_until and expect timeout was broken by the
2930
fix to Issue #14635 in Python 2.7.4 to be interpreted as milliseconds

Modules/selectmodule.c

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -347,14 +347,32 @@ update_ufd_array(pollObject *self)
347347
assert(i < self->ufd_len);
348348
/* Never overflow */
349349
self->ufds[i].fd = (int)PyInt_AsLong(key);
350-
self->ufds[i].events = (short)PyInt_AsLong(value);
350+
self->ufds[i].events = (short)(unsigned short)PyInt_AsLong(value);
351351
i++;
352352
}
353353
assert(i == self->ufd_len);
354354
self->ufd_uptodate = 1;
355355
return 1;
356356
}
357357

358+
static int
359+
ushort_converter(PyObject *obj, void *ptr)
360+
{
361+
unsigned long uval;
362+
363+
uval = PyLong_AsUnsignedLong(obj);
364+
if (uval == (unsigned long)-1 && PyErr_Occurred())
365+
return 0;
366+
if (uval > USHRT_MAX) {
367+
PyErr_SetString(PyExc_OverflowError,
368+
"Python int too large for C unsigned short");
369+
return 0;
370+
}
371+
372+
*(unsigned short *)ptr = Py_SAFE_DOWNCAST(uval, unsigned long, unsigned short);
373+
return 1;
374+
}
375+
358376
PyDoc_STRVAR(poll_register_doc,
359377
"register(fd [, eventmask] ) -> None\n\n\
360378
Register a file descriptor with the polling object.\n\
@@ -366,12 +384,12 @@ static PyObject *
366384
poll_register(pollObject *self, PyObject *args)
367385
{
368386
PyObject *o, *key, *value;
369-
int fd, events = POLLIN | POLLPRI | POLLOUT;
387+
int fd;
388+
unsigned short events = POLLIN | POLLPRI | POLLOUT;
370389
int err;
371390

372-
if (!PyArg_ParseTuple(args, "O|i:register", &o, &events)) {
391+
if (!PyArg_ParseTuple(args, "O|O&:register", &o, ushort_converter, &events))
373392
return NULL;
374-
}
375393

376394
fd = PyObject_AsFileDescriptor(o);
377395
if (fd == -1) return NULL;
@@ -409,12 +427,12 @@ static PyObject *
409427
poll_modify(pollObject *self, PyObject *args)
410428
{
411429
PyObject *o, *key, *value;
412-
int fd, events;
430+
int fd;
431+
unsigned short events;
413432
int err;
414433

415-
if (!PyArg_ParseTuple(args, "Oi:modify", &o, &events)) {
434+
if (!PyArg_ParseTuple(args, "OO&:modify", &o, ushort_converter, &events))
416435
return NULL;
417-
}
418436

419437
fd = PyObject_AsFileDescriptor(o);
420438
if (fd == -1) return NULL;

0 commit comments

Comments
 (0)