]> granicus.if.org Git - python/commitdiff
Issue #27507: Check for integer overflow in bytearray.extend()
authorMartin Panter <vadmium+py@gmail.com>
Mon, 18 Jul 2016 07:53:13 +0000 (07:53 +0000)
committerMartin Panter <vadmium+py@gmail.com>
Mon, 18 Jul 2016 07:53:13 +0000 (07:53 +0000)
Patch by Xiang Zhang.

Misc/NEWS
Objects/bytearrayobject.c

index 7e762e8853909b89d2db770ffd18e08fec484649..05162779f6954793867d43b46332bd314da64a80 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -13,6 +13,9 @@ Core and Builtins
 - Issue #27473: Fixed possible integer overflow in str, unicode and bytearray
   concatenations and repetitions.  Based on patch by Xiang Zhang.
 
+- Issue #27507: Add integer overflow check in bytearray.extend().  Patch by
+  Xiang Zhang.
+
 - Issue #23908: os functions, open() and the io.FileIO constructor now reject
   unicode paths with embedded null character on Windows instead of silently
   truncating them.
index bf8a74eef9d539986d06b62d929152188c320850..f13bc142c49227c24ca5c4fb8112e604f9d52058 100644 (file)
@@ -2322,7 +2322,17 @@ bytearray_extend(PyByteArrayObject *self, PyObject *arg)
         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);