Skip to content

Commit

Permalink
Fix alignment and size of new types
Browse files Browse the repository at this point in the history
  • Loading branch information
Schamper committed Nov 26, 2023
1 parent 6e597da commit ee0786c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
7 changes: 4 additions & 3 deletions dissect/cstruct/cstruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,15 @@ def _make_array(self, type_: MetaType, num_entries: Optional[UnionHint[int, Expr
return self._make_type(name, bases, size, alignment=type_.alignment, attrs=attrs)

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

def _make_packed_type(self, name: str, packchar: str, base: MetaType, *, alignment: int = None) -> type[Packed]:
def _make_packed_type(self, name: str, packchar: str, base: type, *, alignment: int = None) -> type[Packed]:
return self._make_type(
name,
(base, Packed),
struct.calcsize(packchar),
attrs={"packchar": packchar, "alignment": alignment},
alignment=alignment,
attrs={"packchar": packchar},
)

def _make_enum(self, name: str, type_: MetaType, values: dict[str, int]) -> type[Enum]:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,3 +495,13 @@ def test_array_class_name(cs: cstruct):

assert cs.test.fields[0].type.__name__ == "uint8[2]"
assert cs.test2.fields[1].type.__name__ == "uint8[a + 1]"


def test_size_and_aligment(cs: cstruct):
test = cs._make_int_type("test", 1, False, alignment=8)
assert test.size == 1
assert test.alignment == 8

test = cs._make_packed_type("test", "B", int, alignment=8)
assert test.size == 1
assert test.alignment == 8

0 comments on commit ee0786c

Please sign in to comment.