Skip to content

ctypes: _Pointer.contents setter casts a __class__-spoofed value to CDataObject * #161390

Open

Description

@theosotr

Bug report

Summary

Assigning to a ctypes pointer's .contents crashes the interpreter with SIGSEGV if the value is an ordinary Python object that claims to be the right type.

Expected: a TypeError, from a type check that does not consult __class__.

Reproducer

import ctypes

class Forge:
    __slots__ = ("a", "b", "c", "d")
    __class__ = ctypes.c_int

    def __init__(self):
        self.a = ctypes.c_int(7)
        self.b = self.c = self.d = None

ctypes.pointer(ctypes.c_int(1)).contents = Forge()
Segmentation fault (core dumped)

CPython versions tested on:

3.15

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    type-crashA hard crash of the interpreter, possibly with a core dump
    and removed
    type-bugAn unexpected behavior, bug, or error
    on Nov 5, 2026
  2. makwana-akshat commented on Nov 5, 2026

    @makwana-akshat

    i would like to solve this issue. assign/

  3. added a commit that references this issue on Nov 5, 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

    extension-modulesC modules in the Modules dirtopic-ctypestype-crashA 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