gh-96821: Fix undefined behaviour in _ctypes/cfield.c by matthiasgoergens · Pull Request #96925 · python/cpython (original) (raw)

there are apparently test cases where LOW_BIT(size) = 17

I'm a bit worried that there's a deeper bug here. The relevant test case arises from testing this structure:

class BITS(Structure):
    _fields_ = [("A", c_int, 1),
                ("B", c_int, 2),
                ("C", c_int, 3),
                ("D", c_int, 4),
                ("E", c_int, 5),
                ("F", c_int, 6),
                ("G", c_int, 7),
                ("H", c_int, 8),
                ("I", c_int, 9),

                ("M", c_short, 1),
                ("N", c_short, 2),
                ("O", c_short, 3),
                ("P", c_short, 4),
                ("Q", c_short, 5),
                ("R", c_short, 6),
                ("S", c_short, 7)]

It seems possible that we're trying to cram the bitfield for M into the same int that H and I live in. (With A through G having been put into 28 bits in a separate int.)

In any case, if there is a bug there it would be orthogonal to the fixes in this PR.