Skip to content

gh-151815: Fix segfault in template_iter on allocation failure - #151821

Closed
timurmamedov1 wants to merge 2 commits into
python:mainfrom
timurmamedov1:gh-151815-fix-template-iter-segfault
Closed

timurmamedov1 wants to merge 2 commits into
python:mainfrom
timurmamedov1:gh-151815-fix-template-iter-segfault

Conversation

@timurmamedov1

Copy link
Copy Markdown
Contributor

template_iter() allocates a templateiterobject with PyObject_GC_New (which does not zero memory), but only assigns stringsiter and interpolationsiter after both PyObject_GetIter calls succeed. If either call fails, Py_DECREF(iter) runs templateiter_clear, which calls Py_CLEAR on uninitialized pointers, causing a segfault.

Fix: initialize both fields to NULL immediately after allocation so Py_CLEAR is safe on the error paths.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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’s stringsiter and interpolationsiter to NULL immediately after PyObject_GC_New() so tp_clear/Py_CLEAR is 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 thread Objects/templateobject.c Resolved
@timurmamedov1
timurmamedov1 force-pushed the gh-151815-fix-template-iter-segfault branch 2 times, most recently from 645d1c2 to 01f7dbf Compare June 21, 2026 18:23
@bedevere-app

bedevere-app Bot commented Jun 21, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@timurmamedov1
timurmamedov1 force-pushed the gh-151815-fix-template-iter-segfault branch 2 times, most recently from aa005aa to 5653555 Compare June 21, 2026 20:14
@timurmamedov1
timurmamedov1 force-pushed the gh-151815-fix-template-iter-segfault branch from b6dc145 to 8e5d35c Compare June 24, 2026 20:00
Comment thread Objects/templateobject.c Resolved

@sergey-miryanov sergey-miryanov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Oct 11, 2026
@timurmamedov1

Copy link
Copy Markdown
Contributor Author

@lysnikolaou could you take a look when you get a chance? sergey-miryanov has approved it and the NEWS entry is in. It's a small fix that creates the sub-iterators before allocating the iterator object, so a failed PyObject_GetIter() no longer leaves a half-initialized object to dealloc. Thank you!

@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label Oct 18, 2026
@sergey-miryanov

Copy link
Copy Markdown
Contributor

@timurmamedov1 Could you please fix merge conflicts?

@timurmamedov1

timurmamedov1 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor Author

@sergey-miryanov Thanks for the review! Looks like the issue was fixed in #159773 (gh-159739), which was merged in Oct, so I'm closing this one as superseded.

@timurmamedov1
timurmamedov1 deleted the gh-151815-fix-template-iter-segfault branch November 26, 2026 02:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

3 participants