https://github.com/python/cpython/commit/ebf54596bf9deec056e1a9d5763c0d5a10b6e23a
commit: ebf54596bf9deec056e1a9d5763c0d5a10b6e23a
branch: 3.14
author: Stan Ulbrych <[email protected]>
committer: vstinner <[email protected]>
date: 2025-11-06T16:20:03Z
summary:

[3.14] gh-140939: Fix memory leak in `_PyBytes_FormatEx` error path (GH-140957) 
(#141154)

(cherry picked from commit d6c89a2df2c8b7603125883494e9058a88348f66)

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2025-11-03-17-21-38.gh-issue-140939.FVboAw.rst
M Lib/test/test_bytes.py
M Objects/bytesobject.c

diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
index 5e57b6d0eee5ba..08119f5f769711 100644
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -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
diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2025-11-03-17-21-38.gh-issue-140939.FVboAw.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-11-03-17-21-38.gh-issue-140939.FVboAw.rst
new file mode 100644
index 00000000000000..a2921761f75556
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-11-03-17-21-38.gh-issue-140939.FVboAw.rst
@@ -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`.
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 5dd1a1f09c82f1..eb13f16f67295c 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -975,8 +975,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;

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to