Skip to content

Commit

Permalink
Merge branch 'main' into fix-keyword-name
Browse files Browse the repository at this point in the history
  • Loading branch information
Schamper authored Jan 9, 2024
2 parents 7ce9599 + 7eb3cce commit 3accd56
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions dissect/cstruct/types/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,12 @@ def _calc_size_and_offsets(self) -> None:
size = max(len(field.type), size)
alignment = max(field.alignment, alignment)

if self.align and size is not None:
# Add "tail padding" if we need to align
# This bit magic rounds up to the next alignment boundary
# E.g. offset = 3; alignment = 8; -offset & (alignment - 1) = 5
size += -size & (alignment - 1)

self.size = size
self.alignment = alignment

Expand Down
15 changes: 15 additions & 0 deletions tests/test_align.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ def test_align_union():
assert obj.dumps() == buf


def test_align_union_tail():
d = """
union test {
uint64 a;
uint32 b[3];
};
"""
c = cstruct.cstruct()
c.load(d, align=True)

assert c.test.align
assert c.test.alignment == 8
assert c.test.size == 16


def test_align_array():
d = """
struct test {
Expand Down

0 comments on commit 3accd56

Please sign in to comment.