Skip to content

Commit

Permalink
Make nested structure definitions with an array work as expected (#99)
Browse files Browse the repository at this point in the history
(DIS-2591)
  • Loading branch information
cecinestpasunepipe authored Aug 14, 2024
1 parent 7f6a584 commit 046dfa5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion dissect/cstruct/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,10 @@ def _parse_field(self, tokens: TokenConsumer) -> Field:
type_ = self.cstruct.resolve(self._identifier(tokens))
elif tokens.next == self.TOK.STRUCT:
type_ = self._struct(tokens)

if tokens.next != self.TOK.NAME:
return Field(type_.__name__, type_)
type_, name, bits = self._parse_field_type(type_, type_.__name__)
return Field(name.strip(), type_, bits)

if tokens.next != self.TOK.NAME:
raise ParserError(f"line {self._lineno(tokens.next)}: expected name")
Expand Down
16 changes: 16 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@
from dissect.cstruct.types import ArrayMetaType, Pointer


def test_nested_structs(cs: cstruct, compiled: bool) -> None:
cdef = """
struct nest {
struct {
uint32 b;
} a[4];
};
"""

cs.load(cdef, compiled=compiled)
data = b"\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00"
obj = cs.nest(data)
for i in range(0, 4):
assert obj.a[i].b == i


def test_preserve_comment_newlines() -> None:
cdef = """
// normal comment
Expand Down

0 comments on commit 046dfa5

Please sign in to comment.