Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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 7 Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,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`.
4 changes: 3 additions & 1 deletion 4 Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -965,8 +965,10 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
/* 2: size preallocated for %s */
if (alloc > 2) {
res = _PyBytesWriter_Prepare(&writer, res, alloc - 2);
if (res == NULL)
if (res == NULL) {
Py_XDECREF(temp);
goto error;
}
}
#ifndef NDEBUG
char *before = res;
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.