mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
[3.14] gh-140939: Fix memory leak in _PyBytes_FormatEx error path (GH-140957) (#141154)
(cherry picked from commit d6c89a2df2)
This commit is contained in:
parent
5d10409e33
commit
ebf54596bf
3 changed files with 12 additions and 1 deletions
|
|
@ -802,6 +802,13 @@ def __int__(self):
|
||||||
with self.assertRaisesRegex(TypeError, msg):
|
with self.assertRaisesRegex(TypeError, msg):
|
||||||
operator.mod(format_bytes, value)
|
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):
|
def test_imod(self):
|
||||||
b = self.type2test(b'hello, %b!')
|
b = self.type2test(b'hello, %b!')
|
||||||
orig = b
|
orig = b
|
||||||
|
|
|
||||||
|
|
@ -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`.
|
||||||
|
|
@ -975,8 +975,10 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
|
||||||
/* 2: size preallocated for %s */
|
/* 2: size preallocated for %s */
|
||||||
if (alloc > 2) {
|
if (alloc > 2) {
|
||||||
res = _PyBytesWriter_Prepare(&writer, res, alloc - 2);
|
res = _PyBytesWriter_Prepare(&writer, res, alloc - 2);
|
||||||
if (res == NULL)
|
if (res == NULL) {
|
||||||
|
Py_XDECREF(temp);
|
||||||
goto error;
|
goto error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
char *before = res;
|
char *before = res;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue