]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-142433: Move deref to below the error when checking for laststring (#142402)
authorAZero13 <gfunni234@gmail.com>
Wed, 10 Dec 2025 15:41:52 +0000 (10:41 -0500)
committerGitHub <noreply@github.com>
Wed, 10 Dec 2025 15:41:52 +0000 (16:41 +0100)
Move deref of laststring to below the error checking so the deref
is applied after the object in strings is replaced.

Misc/NEWS.d/next/Core_and_Builtins/2025-12-08-17-34-57.gh-issue-142402.iV0ON3.rst [new file with mode: 0644]
Objects/templateobject.c

diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-12-08-17-34-57.gh-issue-142402.iV0ON3.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-12-08-17-34-57.gh-issue-142402.iV0ON3.rst
new file mode 100644 (file)
index 0000000..bad3147
--- /dev/null
@@ -0,0 +1,3 @@
+Fix reference counting when adjacent literal parts are merged while constructing
+:class:`string.templatelib.Template`, preventing the displaced string object
+from leaking.
index ac38e4de435d5d5374bce19e4848b059283ef4cc..a05208e4c8fc8e8f4ad0c520c231dd461cd47dd3 100644 (file)
@@ -148,13 +148,14 @@ template_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
             if (last_was_str) {
                 PyObject *laststring = PyTuple_GET_ITEM(strings, stringsidx - 1);
                 PyObject *concat = PyUnicode_Concat(laststring, item);
-                Py_DECREF(laststring);
                 if (!concat) {
                     Py_DECREF(strings);
                     Py_DECREF(interpolations);
                     return NULL;
                 }
+                /* Replace laststring with concat */
                 PyTuple_SET_ITEM(strings, stringsidx - 1, concat);
+                Py_DECREF(laststring);
             }
             else {
                 PyTuple_SET_ITEM(strings, stringsidx++, Py_NewRef(item));