Skip to content

Iterating over ctypes pointer causes hang or segmentation fault #92347

Description

@chgnrdv

Trying to create iterable (list, tuple, set etc.) from PyCPointer instance leads to hang or segmentation fault.
This code causes hang and memory leak which goes until oom-killer kills python process:

import ctypes
x = ctypes.c_int32(12345)
buf4_p_t = ctypes.POINTER(ctypes.ARRAY(ctypes.c_byte, 4))
buf_p = ctypes.cast(ctypes.byref(x), buf4_p_t)
list(buf_p)

Similar code that causes segmentation fault:

import ctypes
x = ctypes.c_int32(54321)
xp = ctypes.pointer(x)
list(xp)

Iterating over pointer using for statement leads to an infinite loop:
for i in buf_p: print(i)
for i in xp: print(i)
list(for i in buf_p)

Tried on 3.10.3 and 3.11.0a6+, fails at both.

The possible reason is that PyCPointer_Type defines sq_item without defining tp_iter.
Despite I can't imagine any reason why anyone might need to use pointer as iterable/sequence, raising TypeError in such situation would be a better option.

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on May 5, 2022
  2. JelleZijlstra commented on May 5, 2022

    @JelleZijlstra
    Member

    Similar to #92262. I think we should fix this (by setting tp_iter to None) but only on the main branch in case people rely on calling next() on a pointer object.

  3. jkloth commented on May 6, 2022

    @jkloth
    Contributor

    Do note that indexed access to pointer objects is a documented (and useful) behavior. It would be quite surprising to have a sequence-like object raise an exception when used in a for loop, typing constructs notwithstanding.

    Perhaps a note in the Pointers section mentioning the unbounded behavior when iterating aka an infinite generator.

  4. added
    type-crashA hard crash of the interpreter, possibly with a core dump
    and removed
    type-bugAn unexpected behavior, bug, or error
    on Jul 3, 2026
  5. added 3 commits that reference this issue on Sep 21, 2026
  6. added a commit that references this issue on Sep 28, 2026
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

    topic-ctypestype-crashA hard crash of the interpreter, possibly with a core dump

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions