[Python-checkins] bpo-35972: _xxsubinterpreters: Fix potential integer truncation on 32-bit in channel_send() (gh-11822)

Eric Snow webhook-mailer at python.org
Tue Feb 12 11:06:48 EST 2019


https://github.com/python/cpython/commit/16f842da3c862d76a1177bd8ef9579703c24fa5a
commit: 16f842da3c862d76a1177bd8ef9579703c24fa5a
branch: master
author: Alexey Izbyshev <izbyshev at ispras.ru>
committer: Eric Snow <ericsnowcurrently at gmail.com>
date: 2019-02-12T09:06:43-07:00
summary:

bpo-35972: _xxsubinterpreters: Fix potential integer truncation on 32-bit in channel_send() (gh-11822)

files:
M Lib/test/test__xxsubinterpreters.py
M Python/pystate.c

diff --git a/Lib/test/test__xxsubinterpreters.py b/Lib/test/test__xxsubinterpreters.py
index 26032d6c85f2..1eece9659249 100644
--- a/Lib/test/test__xxsubinterpreters.py
+++ b/Lib/test/test__xxsubinterpreters.py
@@ -393,7 +393,19 @@ def test_bytes(self):
                             for i in range(-1, 258))
 
     def test_int(self):
-        self._assert_values(range(-1, 258))
+        self._assert_values(itertools.chain(range(-1, 258),
+                                            [sys.maxsize, -sys.maxsize - 1]))
+
+    def test_non_shareable_int(self):
+        ints = [
+            sys.maxsize + 1,
+            -sys.maxsize - 2,
+            2**1000,
+        ]
+        for i in ints:
+            with self.subTest(i):
+                with self.assertRaises(OverflowError):
+                    interpreters.channel_send(self.cid, i)
 
 
 ##################################
diff --git a/Python/pystate.c b/Python/pystate.c
index 4dc3b81e4cdb..f0b2a6729f33 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -1467,13 +1467,17 @@ _str_shared(PyObject *obj, _PyCrossInterpreterData *data)
 static PyObject *
 _new_long_object(_PyCrossInterpreterData *data)
 {
-    return PyLong_FromLongLong((intptr_t)(data->data));
+    return PyLong_FromSsize_t((Py_ssize_t)(data->data));
 }
 
 static int
 _long_shared(PyObject *obj, _PyCrossInterpreterData *data)
 {
-    int64_t value = PyLong_AsLongLong(obj);
+    /* Note that this means the size of shareable ints is bounded by
+     * sys.maxsize.  Hence on 32-bit architectures that is half the
+     * size of maximum shareable ints on 64-bit.
+     */
+    Py_ssize_t value = PyLong_AsSsize_t(obj);
     if (value == -1 && PyErr_Occurred()) {
         if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
             PyErr_SetString(PyExc_OverflowError, "try sending as bytes");



More information about the Python-checkins mailing list