Skip to content

Commit bb7359a

Browse files
Fix memory leak in template concat error handling (#59)
Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
1 parent 422a515 commit bb7359a

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

Objects/templateobject.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ template_from_strings_interpolations(PyTypeObject *type, PyObject *strings, PyOb
204204
return NULL;
205205
}
206206

207-
((templateobject *) template)->strings = strings;
208-
((templateobject *) template)->interpolations = interpolations;
207+
((templateobject *) template)->strings = Py_NewRef(strings);
208+
((templateobject *) template)->interpolations = Py_NewRef(interpolations);
209209
return template;
210210
}
211211

@@ -337,7 +337,12 @@ template_concat_templates(templateobject *self, templateobject *other)
337337
return NULL;
338338
}
339339

340-
return template_from_strings_interpolations(Py_TYPE(self), newstrings, newinterpolations);
340+
PyObject *newtemplate = template_from_strings_interpolations(Py_TYPE(self), newstrings, newinterpolations);
341+
342+
Py_DECREF(newstrings);
343+
Py_DECREF(newinterpolations);
344+
345+
return newtemplate;
341346
}
342347

343348
static PyObject *
@@ -354,7 +359,12 @@ template_concat_template_str(templateobject *self, PyObject *other)
354359
return NULL;
355360
}
356361

357-
return template_from_strings_interpolations(Py_TYPE(self), newstrings, newinterpolations);
362+
PyObject *newtemplate = template_from_strings_interpolations(Py_TYPE(self), newstrings, newinterpolations);
363+
364+
Py_DECREF(newstrings);
365+
Py_DECREF(newinterpolations);
366+
367+
return newtemplate;
358368
}
359369

360370
static PyObject *
@@ -371,7 +381,12 @@ template_concat_str_template(templateobject *self, PyObject *other)
371381
return NULL;
372382
}
373383

374-
return template_from_strings_interpolations(Py_TYPE(self), newstrings, newinterpolations);
384+
PyObject *newtemplate = template_from_strings_interpolations(Py_TYPE(self), newstrings, newinterpolations);
385+
386+
Py_DECREF(newstrings);
387+
Py_DECREF(newinterpolations);
388+
389+
return newtemplate;
375390
}
376391

377392
PyObject *

0 commit comments

Comments
 (0)