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

Skip to content

Commit ef64847

Browse files
committed
Issue #23985: Fix a possible buffer overrun when deleting a slice from the front of a bytearray and then appending some other bytes data.
Patch by Martin Panter.
2 parents 94e44ed + 2545411 commit ef64847

3 files changed

Lines changed: 21 additions & 6 deletions

File tree

Lib/test/test_bytes.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,22 @@ def test_setslice_extend(self):
993993
b.extend(range(100, 110))
994994
self.assertEqual(list(b), list(range(10, 110)))
995995

996+
def test_fifo_overrun(self):
997+
# Test for issue #23985, a buffer overrun when implementing a FIFO
998+
# Build Python in pydebug mode for best results.
999+
b = bytearray(10)
1000+
b.pop() # Defeat expanding buffer off-by-one quirk
1001+
del b[:1] # Advance start pointer without reallocating
1002+
b += bytes(2) # Append exactly the number of deleted bytes
1003+
del b # Free memory buffer, allowing pydebug verification
1004+
1005+
def test_del_expand(self):
1006+
# Reducing the size should not expand the buffer (issue #23985)
1007+
b = bytearray(10)
1008+
size = sys.getsizeof(b)
1009+
del b[:1]
1010+
self.assertLessEqual(sys.getsizeof(b), size)
1011+
9961012
def test_extended_set_del_slice(self):
9971013
indices = (0, None, 1, 3, 19, 300, 1<<333, -1, -2, -31, -300)
9981014
for start in indices:

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Release date: 2015-05-24
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #23985: Fix a possible buffer overrun when deleting a slice from
14+
the front of a bytearray and then appending some other bytes data.
15+
1316
- Issue #24102: Fixed exception type checking in standard error handlers.
1417

1518
- Issue #15027: The UTF-32 encoder is now 3x to 7x faster.

Objects/bytearrayobject.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
187187
return -1;
188188
}
189189

190-
if (size + logical_offset + 1 < alloc) {
190+
if (size + logical_offset + 1 <= alloc) {
191191
/* Current buffer is large enough to host the requested size,
192192
decide on a strategy. */
193193
if (size < alloc / 2) {
@@ -331,11 +331,7 @@ bytearray_iconcat(PyByteArrayObject *self, PyObject *other)
331331
PyBuffer_Release(&vo);
332332
return PyErr_NoMemory();
333333
}
334-
if (size < self->ob_alloc) {
335-
Py_SIZE(self) = size;
336-
PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0'; /* Trailing null byte */
337-
}
338-
else if (PyByteArray_Resize((PyObject *)self, size) < 0) {
334+
if (PyByteArray_Resize((PyObject *)self, size) < 0) {
339335
PyBuffer_Release(&vo);
340336
return NULL;
341337
}

0 commit comments

Comments
 (0)