summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Panter <vadmium+py@gmail.com>2016-07-18 07:53:13 (GMT)
committerMartin Panter <vadmium+py@gmail.com>2016-07-18 07:53:13 (GMT)
commit371731ebfdc3ee754af8cda72dc7ee5442c7491b (patch)
tree641dbeb4e984a2acd8e121986251c740303f1aba
parent5852fa3f7202899d62a1f1d75d922f718784df7a (diff)
downloadcpython-371731ebfdc3ee754af8cda72dc7ee5442c7491b.zip
cpython-371731ebfdc3ee754af8cda72dc7ee5442c7491b.tar.gz
cpython-371731ebfdc3ee754af8cda72dc7ee5442c7491b.tar.bz2
Issue #27507: Check for integer overflow in bytearray.extend()
Patch by Xiang Zhang.
-rw-r--r--Misc/NEWS3
-rw-r--r--Objects/bytearrayobject.c12
2 files changed, 14 insertions, 1 deletions
diff --git a/Misc/NEWS b/Misc/NEWS
index 770f159..1151824 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -22,6 +22,9 @@ Core and Builtins
- 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
index b21acfb..1458635 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -2474,7 +2474,17 @@ bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
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);