Skip to content

Commit 16f842d

Browse files
Alexey Izbyshevericsnowcurrently
authored andcommitted
bpo-35972: _xxsubinterpreters: Fix potential integer truncation on 32-bit in channel_send() (gh-11822)
1 parent b01786c commit 16f842d

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

Lib/test/test__xxsubinterpreters.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,19 @@ def test_bytes(self):
393393
for i in range(-1, 258))
394394

395395
def test_int(self):
396-
self._assert_values(range(-1, 258))
396+
self._assert_values(itertools.chain(range(-1, 258),
397+
[sys.maxsize, -sys.maxsize - 1]))
398+
399+
def test_non_shareable_int(self):
400+
ints = [
401+
sys.maxsize + 1,
402+
-sys.maxsize - 2,
403+
2**1000,
404+
]
405+
for i in ints:
406+
with self.subTest(i):
407+
with self.assertRaises(OverflowError):
408+
interpreters.channel_send(self.cid, i)
397409

398410

399411
##################################

Python/pystate.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,13 +1467,17 @@ _str_shared(PyObject *obj, _PyCrossInterpreterData *data)
14671467
static PyObject *
14681468
_new_long_object(_PyCrossInterpreterData *data)
14691469
{
1470-
return PyLong_FromLongLong((intptr_t)(data->data));
1470+
return PyLong_FromSsize_t((Py_ssize_t)(data->data));
14711471
}
14721472

14731473
static int
14741474
_long_shared(PyObject *obj, _PyCrossInterpreterData *data)
14751475
{
1476-
int64_t value = PyLong_AsLongLong(obj);
1476+
/* Note that this means the size of shareable ints is bounded by
1477+
* sys.maxsize. Hence on 32-bit architectures that is half the
1478+
* size of maximum shareable ints on 64-bit.
1479+
*/
1480+
Py_ssize_t value = PyLong_AsSsize_t(obj);
14771481
if (value == -1 && PyErr_Occurred()) {
14781482
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
14791483
PyErr_SetString(PyExc_OverflowError, "try sending as bytes");

0 commit comments

Comments
 (0)