gh-142433: Move deref to below the error when checking for laststring (#142402)

Move deref of laststring to below the error checking so the deref
is applied after the object in strings is replaced.
This commit is contained in:
AZero13 2025-12-10 10:41:52 -05:00 committed by GitHub
parent c76cfe8d89
commit 785268fdce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 5 additions and 1 deletions

View File

@ -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.

View 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));