ctypes.Structure: bitfield of underaligned type can cause read of unrelated memory #130410
Labels
extension-modules
C modules in the Modules dir
topic-ctypes
type-bug
An unexpected behavior, bug, or error
Uh oh!
There was an error while loading. Please reload this page.
Bug report
Bug description:
(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:This matches GCC struct layout:
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.CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
The text was updated successfully, but these errors were encountered: