Skip to content

_align_ = 0 segfaults when used with empty filed list (_fields_ = []) #132673

Closed

Description

@karas84

Crash report

What happened?

Tested on the latest ubuntu:25.04 docker image.

from ctypes import Structure

class MyStructure(Structure):
    _align_ = 0
    _fields_ = []

Crashes with Floating point exception on CPython 3.13.

I'm attaching both gdb core dump and backtrace: dump.zip

Problem seems caused by _ctypes/stgdict.c:573:
aligned_size = ((size + total_align - 1) / total_align) * total_align;

Also tested on CPython 3.14, which doesn't segfaults but exits on an assert in ctypes/_layout.py:19:
assert multiple > 0

called by ctypes/_layout.py:314:
aligned_size = round_up(total_size, align)

CPython versions tested on:

3.13

Operating systems tested on:

Linux

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

Python 3.13.3 (main, Apr 8 2025, 19:55:40) [GCC 14.2.0]

Linked PRs

Activity

  1. added
    type-crash
    on Apr 18, 2025
  2. added
    extension-modules
    3.13
    3.14
    on Apr 18, 2025
  3. self-assigned this
    on Apr 18, 2025
  4. sobolevn commented on Apr 18, 2025

    @sobolevn
    Member

    This is a really funny bug, thank you :)
    I found what's wrong.

  5. added 2 commits that reference this issue on Apr 18, 2025
  6. sobolevn commented on Apr 18, 2025

    @sobolevn
    Member

    I can't reproduce this failure on the latest 3.13.

    Try this:

    1. git checkout 3.13
    2. ./configure --with-pydebug && make
    3. ./python
    4. Copy paste your code

    For me this happens:

    » ./python.exe                       
    Python 3.13.3+ (heads/backport-678b8e1-3.13-dirty:ca2e1a1e1c1, Apr 18 2025, 17:37:16) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from ctypes import Structure
    ... 
    ... class MyStructure(Structure):
    ...     _align_ = 0
    ...     _fields_ = []
    ...     
    >>> MyStructure
    <class '__main__.MyStructure'>
    

    So, looks like it was fixed already on 3.13 and I fixed it on 3.14

  7. karas84 commented on Apr 18, 2025

    @karas84
    Author

    Just tried on Linux:

    ./python 
    Python 3.13.3+ (heads/3.13:ca2e1a1e1c1, Apr 18 2025, 16:53:58) [GCC 13.3.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from ctypes import Structure
    ... 
    ... class MyStructure(Structure):
    ...     _align_ = 0
    ...     _fields_ = []
    ...     
    Floating point exception
    

    Note that I'm using GCC while you are using CLANG, maybe that's the difference (or maybe win vs linux)?

  8. ZeroIntensity commented on Apr 18, 2025

    @ZeroIntensity
    Member

    I can reproduce this on a 3.13.3 release build. We need a 3.13 backport.

  9. sobolevn commented on Apr 18, 2025

    @sobolevn
    Member

    @ZeroIntensity would you like to create a PR for that? It will be a different one completely (C vs Python implementation). Since I can't reproduce - it would be hard for me to test this.

  10. added a commit that references this issue on Apr 19, 2025
  11. ZeroIntensity commented on Apr 19, 2025

    @ZeroIntensity
    Member

    Thanks @karas84 for the report!

  12. karas84 commented on Apr 20, 2025

    @karas84
    Author

    My pleasure!

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

Metadata

Metadata

Assignees

Labels

3.133.14extension-modulestopic-ctypestype-crash

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions