You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ctypes.Structure: bitfield of underaligned type can cause read of unrelated memory #130410
(This is an exotic edge case found by tests that are perhaps too stringent, but, we prefer issues over XXX comments in the code, so, here goes.)
A bitfield of an “underaligned” type (one whose alignment is smaller than its size) can cause the “storage unit” that ctypes uses for handling the bitfield to extend past the containing Structure.
For example, on 32-bit x86 architecture, where int64_t is 8 bytes long but only aligned to 4 bytes, we have:
ctypes handles bitfield reads/writes by reading the entire storage unit, masking/shifting, and (for writes) writing the entire unit back. So, in this case it can read/write memory that doesn't belong to the struct.
The bug is still present on current main. FIXINT_GETSET/FIXINT_GETSET_SW in cfield.c both do memcpy(&val, ptr, sizeof(val)), reads the full 8 bytes even when _layout.py gives the struct only 4 bytes on 32-bit. ASAN confirms the OOB. I have a fix ready (use actual storage size instead of sizeof(CTYPE) and tests for underaligned case). Will make PR if this is worth fixing now?
Activity
BHUVANSH855 commentedon Oct 20, 2026
The bug is still present on current main.
FIXINT_GETSET/FIXINT_GETSET_SWincfield.cboth domemcpy(&val, ptr, sizeof(val)), reads the full 8 bytes even when_layout.pygives the struct only 4 bytes on 32-bit. ASAN confirms the OOB. I have a fix ready (use actual storage size instead ofsizeof(CTYPE)and tests for underaligned case). Will make PR if this is worth fixing now?as per @encukou said "it's not worth to fix now", a year ago.