gh-151815: Fix segfault in template_iter on allocation failure#151821
Open
timurmamedov1 wants to merge 1 commit into
Open
gh-151815: Fix segfault in template_iter on allocation failure#151821timurmamedov1 wants to merge 1 commit into
timurmamedov1 wants to merge 1 commit into
Conversation
Initialize stringsiter and interpolationsiter to NULL immediately after PyObject_GC_New so that templateiter_clear can safely run Py_CLEAR if a subsequent PyObject_GetIter call fails.
There was a problem hiding this comment.
Pull request overview
Fixes a crash in the C implementation of t-string Template iteration by making the iterator object safe to deallocate when construction fails partway through (e.g., on PyObject_GetIter() allocation failure).
Changes:
- Initialize
templateiterobject’sstringsiterandinterpolationsitertoNULLimmediately afterPyObject_GC_New()sotp_clear/Py_CLEARis safe on error paths. - Add a NEWS blurb documenting the crash fix.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
Objects/templateobject.c |
Prevents segfault by NULL-initializing iterator fields before any fallible operations. |
Misc/NEWS.d/next/Core_and_Builtins/2026-06-20-14-00-00.gh-issue-151815.TmplIt.rst |
Documents the crash fix in the Core and Builtins NEWS entries. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
222
to
+230
| template_iter(PyObject *op) | ||
| { | ||
| templateobject *self = templateobject_CAST(op); | ||
| templateiterobject *iter = PyObject_GC_New(templateiterobject, &_PyTemplateIter_Type); | ||
| if (iter == NULL) { | ||
| return NULL; | ||
| } | ||
| iter->stringsiter = NULL; | ||
| iter->interpolationsiter = NULL; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
template_iter()allocates atemplateiterobjectwithPyObject_GC_New(which does not zero memory), but only assignsstringsiterandinterpolationsiterafter bothPyObject_GetItercalls succeed. If either call fails,Py_DECREF(iter)runstemplateiter_clear, which callsPy_CLEARon uninitialized pointers, causing a segfault.Fix: initialize both fields to
NULLimmediately after allocation soPy_CLEARis safe on the error paths.