Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,13 @@ def __int__(self):
with self.assertRaisesRegex(TypeError, msg):
operator.mod(format_bytes, value)

def test_memory_leak_gh_140939(self):
# gh-140939: MemoryError is raised without leaking
_testcapi = import_helper.import_module('_testcapi')
with self.assertRaises(MemoryError):
b = self.type2test(b'%*b')
b % (_testcapi.PY_SSIZE_T_MAX, b'abc')

def test_imod(self):
b = self.type2test(b'hello, %b!')
orig = b
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix memory leak when :class:`bytearray` or :class:`bytes` is formated with the
``%*b`` format with a large width that results in a :exc:`MemoryError`.
1 change: 1 addition & 0 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,7 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
if (alloc > 2) {
res = PyBytesWriter_GrowAndUpdatePointer(writer, alloc - 2, res);
if (res == NULL) {
Py_XDECREF(temp);
goto error;
}
}
Expand Down
Loading