Skip to content

[3.13] gh-132673: Fix crash with zero-alignment in a ctypes.Structure #132695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion Lib/test/test_ctypes/test_aligned_structures.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from ctypes import (
c_char, c_uint32, c_uint16, c_ubyte, c_byte, alignment, sizeof,
BigEndianStructure, LittleEndianStructure,
BigEndianUnion, LittleEndianUnion,
BigEndianUnion, LittleEndianUnion, Structure
)
import struct
import unittest
Expand Down Expand Up @@ -281,6 +281,41 @@ class Main(sbase):
self.assertEqual(main.b.y, 3)
self.assertEqual(main.c, 4)

def test_negative_align(self):
for base in (Structure, LittleEndianStructure, BigEndianStructure):
with (
self.subTest(base=base),
self.assertRaisesRegex(
ValueError,
'_align_ must be a non-negative integer',
)
):
class MyStructure(base):
_align_ = -1
_fields_ = []

def test_zero_align_no_fields(self):
for base in (Structure, LittleEndianStructure, BigEndianStructure):
with self.subTest(base=base):
class MyStructure(base):
_align_ = 0
_fields_ = []

self.assertEqual(alignment(MyStructure), 1)
self.assertEqual(alignment(MyStructure()), 1)

def test_zero_align_with_fields(self):
for base in (Structure, LittleEndianStructure, BigEndianStructure):
with self.subTest(base=base):
class MyStructure(base):
_align_ = 0
_fields_ = [
("x", c_ubyte),
]

self.assertEqual(alignment(MyStructure), 1)
self.assertEqual(alignment(MyStructure()), 1)


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash when using ``_align_ = 0`` and ``_fields_ = []`` in a
:class:`ctypes.Structure`.
3 changes: 2 additions & 1 deletion Modules/_ctypes/stgdict.c
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ PyCStructUnionType_update_stginfo(PyObject *type, PyObject *fields, int isStruct
size = 0;
align = 0;
union_size = 0;
total_align = forced_alignment;
total_align = forced_alignment == 0 ? 1 : forced_alignment;
stginfo->ffi_type_pointer.type = FFI_TYPE_STRUCT;
stginfo->ffi_type_pointer.elements = PyMem_New(ffi_type *, len + 1);
if (stginfo->ffi_type_pointer.elements == NULL) {
Expand Down Expand Up @@ -570,6 +570,7 @@ PyCStructUnionType_update_stginfo(PyObject *type, PyObject *fields, int isStruct
}

/* Adjust the size according to the alignment requirements */
assert(total_align != 0);
aligned_size = ((size + total_align - 1) / total_align) * total_align;

if (isStruct) {
Expand Down
Loading