# HG changeset patch # Parent bc1a178b3bc8b63feebb3f7021813247e4c7f513 diff -r bc1a178b3bc8 Lib/test/test_bytes.py --- a/Lib/test/test_bytes.py Sat Apr 18 05:54:02 2015 +0200 +++ b/Lib/test/test_bytes.py Tue May 19 14:12:13 2015 +0000 @@ -985,6 +985,22 @@ b.extend(range(100, 110)) self.assertEqual(list(b), list(range(10, 110))) + def test_fifo_overrun(self): + # Test for issue #23985, a buffer overrun when implementing a FIFO + # Build Python with "./configure --with-pydebug" for best results + b = bytearray(10) + b.pop() # Defeat expanding buffer off-by-one quirk + del b[:1] # Advance start pointer without reallocating + b += bytes(2) # Append exactly the number of deleted bytes + del b # Free memory buffer, allowing pydebug verification + + def test_del_expand(self): + # Reducing the size should not expand the buffer (issue #23985) + b = bytearray(10) + size = sys.getsizeof(b) + del b[:1] + self.assertLessEqual(sys.getsizeof(b), size) + def test_extended_set_del_slice(self): indices = (0, None, 1, 3, 19, 300, 1<<333, -1, -2, -31, -300) for start in indices: diff -r bc1a178b3bc8 Objects/bytearrayobject.c --- a/Objects/bytearrayobject.c Sat Apr 18 05:54:02 2015 +0200 +++ b/Objects/bytearrayobject.c Tue May 19 14:12:13 2015 +0000 @@ -186,7 +186,7 @@ return -1; } - if (size + logical_offset + 1 < alloc) { + if (size + logical_offset + 1 <= alloc) { /* Current buffer is large enough to host the requested size, decide on a strategy. */ if (size < alloc / 2) { @@ -330,11 +330,7 @@ PyBuffer_Release(&vo); return PyErr_NoMemory(); } - if (size < self->ob_alloc) { - Py_SIZE(self) = size; - PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0'; /* Trailing null byte */ - } - else if (PyByteArray_Resize((PyObject *)self, size) < 0) { + if (PyByteArray_Resize((PyObject *)self, size) < 0) { PyBuffer_Release(&vo); return NULL; }