Skip to content

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

Closed
@karas84

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

Metadata

Metadata

Assignees

Labels

3.13bugs and security fixes3.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