Skip to content

Commit 66d0c10

Browse files
dhylandspfalcon
authored andcommitted
extmod: Fix uctypes size calculation for bitfields
1 parent 8175877 commit 66d0c10

3 files changed

Lines changed: 12 additions & 1 deletion

File tree

extmod/moductypes.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ STATIC mp_uint_t uctypes_struct_size(mp_obj_t desc_in, int layout_type, mp_uint_
230230
mp_uint_t offset = MP_OBJ_SMALL_INT_VALUE(v);
231231
mp_uint_t val_type = GET_TYPE(offset, VAL_TYPE_BITS);
232232
offset &= VALUE_MASK(VAL_TYPE_BITS);
233+
if (val_type >= BFUINT8 && val_type <= BFINT32) {
234+
offset &= (1 << OFFSET_BITS) - 1;
235+
}
233236
mp_uint_t s = uctypes_struct_scalar_size(val_type);
234237
if (s > *max_field_size) {
235238
*max_field_size = s;

tests/extmod/uctypes_sizeof.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
# arr2 is array at offset 0, size 2, of structures defined recursively
77
"arr2": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0}),
88
"arr3": (uctypes.ARRAY | 2, uctypes.UINT16 | 2),
9-
"arr4": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0, "w": uctypes.UINT16 | 1})
9+
"arr4": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0, "w": uctypes.UINT16 | 1}),
10+
"sub": (0, {
11+
'b1': uctypes.BFUINT8 | 0 | 4 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
12+
'b2': uctypes.BFUINT8 | 0 | 0 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
13+
}),
1014
}
1115

1216
data = bytearray(b"01234567")
@@ -29,3 +33,6 @@
2933
print(uctypes.sizeof(S.arr4))
3034
assert uctypes.sizeof(S.arr4) == 6
3135

36+
print(uctypes.sizeof(S.sub))
37+
assert uctypes.sizeof(S.sub) == 1
38+

tests/extmod/uctypes_sizeof.py.exp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
4
44
TypeError
55
6
6+
1

0 commit comments

Comments
 (0)