[Python-checkins] cpython (3.5): Issue #27507: Check for integer overflow in bytearray.extend()

martin.panter python-checkins at python.org
Mon Jul 18 04:22:25 EDT 2016


https://hg.python.org/cpython/rev/54cc0480904c
changeset:   102396:54cc0480904c
branch:      3.5
parent:      102394:610b6a5d0c34
user:        Martin Panter <vadmium+py at gmail.com>
date:        Mon Jul 18 07:53:13 2016 +0000
summary:
  Issue #27507: Check for integer overflow in bytearray.extend()

Patch by Xiang Zhang.

files:
  Misc/NEWS                 |   3 +++
  Objects/bytearrayobject.c |  12 +++++++++++-
  2 files changed, 14 insertions(+), 1 deletions(-)


diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -22,6 +22,9 @@
 - Issue #27473: Fixed possible integer overflow in bytes and bytearray
   concatenations.  Patch by Xiang Zhang.
 
+- Issue #27507: Add integer overflow check in bytearray.extend().  Patch by
+  Xiang Zhang.
+
 - Issue #27443: __length_hint__() of bytearray iterators no longer return a
   negative integer for a resized bytearray.
 
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -2474,7 +2474,17 @@
         Py_DECREF(item);
 
         if (len >= buf_size) {
-            buf_size = len + (len >> 1) + 1;
+            Py_ssize_t addition;
+            if (len == PY_SSIZE_T_MAX) {
+                Py_DECREF(it);
+                Py_DECREF(bytearray_obj);
+                return PyErr_NoMemory();
+            }
+            addition = len >> 1;
+            if (addition > PY_SSIZE_T_MAX - len - 1)
+                buf_size = PY_SSIZE_T_MAX;
+            else
+                buf_size = len + addition + 1;
             if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) {
                 Py_DECREF(it);
                 Py_DECREF(bytearray_obj);

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list