← Winnitude! Search  ·  Reader Mode (778 ms)
Links: Stay in Reader Retro Site Converter Retro Mode: OFF Original Page ↗
GITHUB · 20 LINKS

`itertools.groupby`: NULL-pointer dereference (`Py_INCREF(NULL)`) in `groupby_next()` when `__eq__` re-entrantly advances `_grouper` · Issue #163183 · python/cpython

Repository navigation

itertools.groupby: NULL-pointer dereference (Py_INCREF(NULL)) in groupby_next() when __eq__ re-entrantly advances _grouper  #163183


New issue

New issue

Backlog

Backlog
itertools.groupby: NULL-pointer dereference (Py_INCREF(NULL)) in groupby_next() when __eq__ re-entrantly advances _grouper #163183 Labels
extension-modules

type-bug

type-crash

Description

@nevercodecorrect's profile nevercodecorrect opened on Dec 5, 2026

Bug report

Bug Description

In Modules/itertoolsmodule.c, groupby_next() (L566-578 ) takes local strong references to gbo->tgtkey and gbo->currkey before calling PyObject_RichCompareBool(tgtkey, currkey, Py_EQ) (added in gh-143543 / gh-146613 ). However, if __eq__ re-entrantly calls next(g) to obtain a _grouper and advances that _grouper (next(grp)), _grouper_next() executes Py_CLEAR(gbo->currkey) (L711 ), leaving gbo->currkey == NULL.

When the outer __eq__ then returns False (rcmp == 0), groupby_next() breaks out of the for (;;) loop (L578 ) without checking whether gbo->currkey became NULL, and immediately crashes on Py_INCREF(gbo->currkey) (L584 ).

Reproduce

import itertools

g = None
armed = False

class EvilKey:
    def __init__(self, v):
        self.v = v

    def __eq__(self, other):
        global armed
        if armed:
            armed = False
            _, grp2 = next(g)
            try:
                next(grp2)
            except StopIteration:
                pass
            return False
        return isinstance(other, EvilKey) and self.v == other.v

g = itertools.groupby([EvilKey(1), EvilKey(2)])
next(g)
armed = True
try:
    next(g)
except StopIteration:
    pass
print("FINISHED_WITHOUT_TRAP")
./configure --with-address-sanitizer --with-pydebug
make -j"$(nproc)"
PYTHONMALLOC=malloc ASAN_OPTIONS="detect_leaks=0:symbolize=1" ./python reproduce.py

Observed Sanitizer / Crash Output

Verbatim symbolized sanitizer excerpt on stock upstream CPython main (see output.txt for the complete capture):

AddressSanitizer:DEADLYSIGNAL
=================================================================
==60==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x56439b3b8736 bp 0x7fffee960140 sp 0x7fffee9600e0 T0)
==60==The signal is caused by a READ memory access.
==60==Hint: address points to the zero page.
    #0 0x56439b3b8736 in Py_INCREF /third_party/cpython-stock/Include/refcount.h:285:31
    #1 0x56439b3b8736 in groupby_next /third_party/cpython-stock/Modules/itertoolsmodule.c:584:5
    #2 0x56439b0a79aa in builtin_next /third_party/cpython-stock/Python/bltinmodule.c:1756:11
    #3 0x56439ad556b2 in _PyObject_VectorcallTstate //third_party/cpython-stock/Include/internal/pycore_call.h:143:11
    #4 0x56439b0b2d57 in _Py_VectorCallInstrumentation_StackRefSteal /third_party/cpython-stock/Python/ceval.c:755:11
    #5 0x56439aa6b2ef in _PyEval_EvalFrameDefault /third_party/cpython-stock/Python/generated_cases.c.h:1906:35
    ...

==60==Register values:
rax = 0x0000000000000000  rbx = 0x0000512000041160  rcx = 0x0000000000000000  rdx = 0x000056439b8987c0  
rdi = 0x0000000000000000  rsi = 0x000050f000034c10  rbp = 0x00007fffee960140  rsp = 0x00007fffee9600e0  
 r8 = 0x00000ac873760582   r9 = 0x00000ac873760591  r10 = 0x00007fffffffff01  r11 = 0xd3a194c1cde68601  
r12 = 0x0000000000000000  r13 = 0x0000513000018f60  r14 = 0x00000a240000822c  r15 = 0x000050800001cb30  
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /third_party/cpython-stock/Include/refcount.h:285:31 in Py_INCREF

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels


    extension-modules

    type-bug

    type-crash

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions