Skip to content

Commit 0dd1bd1

Browse files
authored
Merge branch '3.13' into backport-7dae107-3.13
2 parents 7bb1d9e + f2665b6 commit 0dd1bd1

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,6 @@ jobs:
597597
- build-macos
598598
- build-ubuntu
599599
- build-ubuntu-ssltests
600-
- build-android
601600
- build-wasi
602601
- test-hypothesis
603602
- build-asan
@@ -610,6 +609,7 @@ jobs:
610609
uses: re-actors/alls-green@05ac9388f0aebcb5727afa17fcccfecd6f8ec5fe
611610
with:
612611
allowed-failures: >-
612+
build-android,
613613
build-windows-msi,
614614
build-ubuntu-ssltests,
615615
test-hypothesis,

Lib/test/test_bytes.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,6 +1941,23 @@ def make_case():
19411941
with self.assertRaises(BufferError):
19421942
ba.rsplit(evil)
19431943

1944+
def test_extend_empty_buffer_overflow(self):
1945+
# gh-143003
1946+
class EvilIter:
1947+
def __iter__(self):
1948+
return self
1949+
def __next__(self):
1950+
return next(source)
1951+
def __length_hint__(self):
1952+
return 0
1953+
1954+
# Use ASCII digits so float() takes the fast path that expects a NUL terminator.
1955+
source = iter(b'42')
1956+
ba = bytearray()
1957+
ba.extend(EvilIter())
1958+
1959+
self.assertRaises(ValueError, float, bytearray())
1960+
19441961
def test_hex_use_after_free(self):
19451962
# Prevent UAF in bytearray.hex(sep) with re-entrant sep.__len__.
19461963
# Regression test for https://github.com/python/cpython/issues/143195.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix an overflow of the shared empty buffer in :meth:`bytearray.extend` when
2+
``__length_hint__()`` returns 0 for non-empty iterator.

Objects/bytearrayobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,7 +1871,6 @@ bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
18711871
Py_DECREF(bytearray_obj);
18721872
return NULL;
18731873
}
1874-
buf[len++] = value;
18751874
Py_DECREF(item);
18761875

18771876
if (len >= buf_size) {
@@ -1881,7 +1880,7 @@ bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
18811880
Py_DECREF(bytearray_obj);
18821881
return PyErr_NoMemory();
18831882
}
1884-
addition = len >> 1;
1883+
addition = len ? len >> 1 : 1;
18851884
if (addition > PY_SSIZE_T_MAX - len - 1)
18861885
buf_size = PY_SSIZE_T_MAX;
18871886
else
@@ -1895,6 +1894,7 @@ bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
18951894
have invalidated it. */
18961895
buf = PyByteArray_AS_STRING(bytearray_obj);
18971896
}
1897+
buf[len++] = value;
18981898
}
18991899
Py_DECREF(it);
19001900

0 commit comments

Comments
 (0)