]> granicus.if.org Git - python/commitdiff
Issue #27581: Don’t rely on overflow wrapping in PySequence_Tuple()
authorMartin Panter <vadmium+py@gmail.com>
Mon, 25 Jul 2016 02:30:05 +0000 (02:30 +0000)
committerMartin Panter <vadmium+py@gmail.com>
Mon, 25 Jul 2016 02:30:05 +0000 (02:30 +0000)
Patch by Xiang Zhang.

Misc/NEWS
Objects/abstract.c

index a9ebb7c0fbe2e8643d2902af4fcb057ed18c73c7..2e28d901060106ef8ae0785b09f674d5f83eb7f4 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -25,6 +25,9 @@ Core and Builtins
 - Issue #27507: Add integer overflow check in bytearray.extend().  Patch by
   Xiang Zhang.
 
+- Issue #27581: Don't rely on wrapping for overflow check in
+  PySequence_Tuple().  Patch by Xiang Zhang.
+
 - Issue #27443: __length_hint__() of bytearray iterators no longer return a
   negative integer for a resized bytearray.
 
index 585992d189e9272c4bdbf3134f86b864a9e4784a..88205bd0ce73caf78b6c2d1a5d20d422c0de9ff0 100644 (file)
@@ -1724,21 +1724,22 @@ PySequence_Tuple(PyObject *v)
             break;
         }
         if (j >= n) {
-            Py_ssize_t oldn = n;
+            size_t newn = (size_t)n;
             /* The over-allocation strategy can grow a bit faster
                than for lists because unlike lists the
                over-allocation isn't permanent -- we reclaim
                the excess before the end of this routine.
                So, grow by ten and then add 25%.
             */
-            n += 10;
-            n += n >> 2;
-            if (n < oldn) {
+            newn += 10u;
+            newn += newn >> 2;
+            if (newn > PY_SSIZE_T_MAX) {
                 /* Check for overflow */
                 PyErr_NoMemory();
                 Py_DECREF(item);
                 goto Fail;
             }
+            n = (Py_ssize_t)newn;
             if (_PyTuple_Resize(&result, n) != 0) {
                 Py_DECREF(item);
                 goto Fail;