GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — itertools.count use-after-free via re-entrant step.__radd__ · Issue #154670 · python/cpython

itertools.count use-after-free via re-entrant step.__radd__ #154670

New issue
Copy link
New issue
Copy link
Open
Open
itertools.count use-after-free via re-entrant step.__radd__#154670
Copy link
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirtype-crashA hard crash of the interpreter, possibly with a core dumpA hard crash of the interpreter, possibly with a core dump

Description

@tonghuaroot
tonghuaroot
opened on Jul 25, 2026
Issue body actions

Bug description

itertools.count has a use-after-free when the step object's __radd__ re-enters the iterator.

count_nextlong() borrows lz->long_cnt (the running total) without Py_INCREF, then calls PyNumber_Add(result, step). If step.__radd__ calls next() on the same count, the inner call moves lz->long_cnt to the new value and hands the old total's only reference back, which is then dropped and freed. The outer call still returns that freed total as a dangling pointer.

The step needs __index__ so count() accepts it as a number, and the returned value has to be used to hit the freed memory:

from itertools import count

class Step:
    armed = True
    def __index__(self):        # so count() accepts it as a number
        return 1
    def __radd__(self, other):
        if Step.armed:
            Step.armed = False
            inner = next(c)     # re-enter; steals the running total's ref
            f"{inner!r}"        # churn the heap so the freed slot is reused
        return other + 1

c = count(1 << 100, Step())
val = next(c)
val + 1                         # use the returned (dangling) total

Run with PYTHONMALLOC=debug python repro.py -> SIGSEGV. With the fix it prints the correct value.

CPython versions tested on

main

Operating systems tested on

macOS

Activity

  1. StanFromIreland
    added
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    type-crashA hard crash of the interpreter, possibly with a core dumpA hard crash of the interpreter, possibly with a core dump
    on Jul 25, 2026
  2. terryjreedy commented on Jul 25, 2026

    @terryjreedy
    terryjreedy
    on Jul 25, 2026
    Member
    More actions

    On Windows main and older and macOS 10.5 13.5,

        c = itertools.count(1 << 100, Step())
    TypeError: a number is required
    

    the step argument has to be a number, and likely an int, not a Step(). How did that run?

  3. tonghuaroot commented on Jul 26, 2026

    @tonghuaroot
    tonghuaroot
    on Jul 26, 2026
    ContributorAuthor
    More actions

    You're right, sorry -- the step has to pass the number check, so a bare class fails. It needs __index__. I have updated the issue with a working reproducer.

    The short version: the step's __radd__ calls next() on the same count. That inner call frees the running total, and the outer call still returns it as a dangling pointer. With PYTHONMALLOC=debug it segfaults on unfixed main; with the fix it prints the right value.

  4. rhettinger commented on Jul 27, 2026

    @rhettinger
    rhettinger
    on Jul 27, 2026
    Contributor
    More actions

    My plan for itertools is to block reentrancy entirely, just like we do for generators.

    If people want concurrent access to itertools (or generators), they can wrap use threading.serialize_iterator to enforce reliable, sequential access.

    Reacted by tonghuaroot (童话)
  5. picnixz commented on Jul 28, 2026

    @picnixz
    picnixz
    on Jul 28, 2026
    Member
    More actions

    @rhettinger I think we should be careful here for existing code that does not crash or segfaults. It could be a breaking change so I believe we can forbid re-entrancy as long as there is no crash.

    I however do not know of any legitimate code that would need such reentrancy (like advancing something twice).

    Maybe a DPO thread or a wider discussion for that (at least an issue) would be great.

    Reacted by tonghuaroot (童话) and Stefan Pochmann
  6. pochmann commented on Jul 28, 2026

    @pochmann
    pochmann
    on Jul 28, 2026
    Contributor
    More actions

    @picnixz Do you mean "can't forbid"?

    Concurrent access isn't the only possible cause for reentry, so I also think a discussion would be good before blocking that.

  7. picnixz commented on Jul 28, 2026

    @picnixz
    picnixz
    on Jul 28, 2026
    Member
    More actions

    Sorry for the typo. Yes I meant that, i.e.: if legitimate code works currently, making reentrancy forbidden will break it and this is usually something we rather want to avoid.

    I think the decision of blocking reentrancy needs to be more motivated and may even need a PEP.

  8. Broyojo
    added a commit that references this issue on Nov 4, 2026

    Itertools: groupby_step failure paths, _grouper(gb, k) reads its key,…

    228713b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirC modules in the Modules dirtype-crashA hard crash of the interpreter, possibly with a core dumpA hard crash of the interpreter, possibly with a core dump

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.