From 046dfa51f0989c7f55cf6d7dd00ba71f3ec5a115 Mon Sep 17 00:00:00 2001 From: cecinestpasunepipe <110607403+cecinestpasunepipe@users.noreply.github.com> Date: Wed, 14 Aug 2024 08:43:57 +0200 Subject: [PATCH] Make nested structure definitions with an array work as expected (#99) (DIS-2591) --- dissect/cstruct/parser.py | 4 +++- tests/test_parser.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/dissect/cstruct/parser.py b/dissect/cstruct/parser.py index 3685444..7634113 100644 --- a/dissect/cstruct/parser.py +++ b/dissect/cstruct/parser.py @@ -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") diff --git a/tests/test_parser.py b/tests/test_parser.py index e81da46..28b7992 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -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