Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 371731e

Browse files
committed
Issue #27507: Check for integer overflow in bytearray.extend()
Patch by Xiang Zhang.
1 parent 5852fa3 commit 371731e

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ Core and Builtins
2222
- Issue #27473: Fixed possible integer overflow in bytes and bytearray
2323
concatenations. Patch by Xiang Zhang.
2424

25+
- Issue #27507: Add integer overflow check in bytearray.extend(). Patch by
26+
Xiang Zhang.
27+
2528
- Issue #27443: __length_hint__() of bytearray iterators no longer return a
2629
negative integer for a resized bytearray.
2730

Objects/bytearrayobject.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2474,7 +2474,17 @@ bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
24742474
Py_DECREF(item);
24752475

24762476
if (len >= buf_size) {
2477-
buf_size = len + (len >> 1) + 1;
2477+
Py_ssize_t addition;
2478+
if (len == PY_SSIZE_T_MAX) {
2479+
Py_DECREF(it);
2480+
Py_DECREF(bytearray_obj);
2481+
return PyErr_NoMemory();
2482+
}
2483+
addition = len >> 1;
2484+
if (addition > PY_SSIZE_T_MAX - len - 1)
2485+
buf_size = PY_SSIZE_T_MAX;
2486+
else
2487+
buf_size = len + addition + 1;
24782488
if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) {
24792489
Py_DECREF(it);
24802490
Py_DECREF(bytearray_obj);

0 commit comments

Comments
 (0)