Skip to content

Commit ee0786c

Browse files
committed
Fix alignment and size of new types
1 parent 6e597da commit ee0786c

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

dissect/cstruct/cstruct.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -332,14 +332,15 @@ def _make_array(self, type_: MetaType, num_entries: Optional[UnionHint[int, Expr
332332
return self._make_type(name, bases, size, alignment=type_.alignment, attrs=attrs)
333333

334334
def _make_int_type(self, name: str, size: int, signed: bool, *, alignment: int = None) -> type[Int]:
335-
return self._make_type(name, (Int,), size, attrs={"signed": signed, "alignment": alignment})
335+
return self._make_type(name, (Int,), size, alignment=alignment, attrs={"signed": signed})
336336

337-
def _make_packed_type(self, name: str, packchar: str, base: MetaType, *, alignment: int = None) -> type[Packed]:
337+
def _make_packed_type(self, name: str, packchar: str, base: type, *, alignment: int = None) -> type[Packed]:
338338
return self._make_type(
339339
name,
340340
(base, Packed),
341341
struct.calcsize(packchar),
342-
attrs={"packchar": packchar, "alignment": alignment},
342+
alignment=alignment,
343+
attrs={"packchar": packchar},
343344
)
344345

345346
def _make_enum(self, name: str, type_: MetaType, values: dict[str, int]) -> type[Enum]:

tests/test_basic.py

+10
Original file line numberDiff line numberDiff line change
@@ -495,3 +495,13 @@ def test_array_class_name(cs: cstruct):
495495

496496
assert cs.test.fields[0].type.__name__ == "uint8[2]"
497497
assert cs.test2.fields[1].type.__name__ == "uint8[a + 1]"
498+
499+
500+
def test_size_and_aligment(cs: cstruct):
501+
test = cs._make_int_type("test", 1, False, alignment=8)
502+
assert test.size == 1
503+
assert test.alignment == 8
504+
505+
test = cs._make_packed_type("test", "B", int, alignment=8)
506+
assert test.size == 1
507+
assert test.alignment == 8

0 commit comments

Comments
 (0)