Skip to content

Commit 785268f

Browse files
authored
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.
1 parent c76cfe8 commit 785268f

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix reference counting when adjacent literal parts are merged while constructing
2+
:class:`string.templatelib.Template`, preventing the displaced string object
3+
from leaking.

Objects/templateobject.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,14 @@ template_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
148148
if (last_was_str) {
149149
PyObject *laststring = PyTuple_GET_ITEM(strings, stringsidx - 1);
150150
PyObject *concat = PyUnicode_Concat(laststring, item);
151-
Py_DECREF(laststring);
152151
if (!concat) {
153152
Py_DECREF(strings);
154153
Py_DECREF(interpolations);
155154
return NULL;
156155
}
156+
/* Replace laststring with concat */
157157
PyTuple_SET_ITEM(strings, stringsidx - 1, concat);
158+
Py_DECREF(laststring);
158159
}
159160
else {
160161
PyTuple_SET_ITEM(strings, stringsidx++, Py_NewRef(item));

0 commit comments

Comments
 (0)