Skip to content

Building a ctypes.CField with wrong byte_size aborts #132470

Closed

Description

@devdanzin

Crash report

What happened?

The following code will cause an abort due to byte_size not matching the size of ctypes.c_byte, failing an assertion that byte_size == info->size.

import ctypes
ctypes.CField(name="a", type=ctypes.c_byte, byte_size=2, byte_offset=2, index=1, _internal_use=True)

Abort message:

python: ./Modules/_ctypes/cfield.c:102: PyCField_new_impl: Assertion `byte_size == info->size' failed.

Aborted (core dumped)

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Output from running 'python -VV' on the command line:

Python 3.14.0a6+ (heads/main:be2d2181e62, Mar 31 2025, 07:30:17) [GCC 11.4.0]

Linked PRs

Activity

  1. added
    type-crashA hard crash of the interpreter, possibly with a core dump
    on Apr 13, 2025
  2. added
    3.14bugs and security fixes
    and removed
    type-crashA hard crash of the interpreter, possibly with a core dump
    on Apr 13, 2025
  3. picnixz commented on Apr 13, 2025

    @picnixz
    Member

    Field wasn't meant to be used publicly for now but, since it's a real crash and if we're to expose this interface in the future, we would be happy to have this check.

  4. added a commit that references this issue on Apr 22, 2025
  5. encukou commented on Apr 28, 2025

    @encukou
    Member

    To set expectations: the CField initializer is indeed not public. If you use _internal_use=True, you are on your own -- don't blame CPython if it crashes.

    With that said, thank you for testing this internal API! And thank you for the fix -- when CField becomes public, there'll be one less thing to do.

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

    3.14bugs and security fixesextension-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