Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dumpstruct for anonymous structures #50

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion dissect/cstruct/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ def _dumpstruct(
ci = 0
out = [f"struct {structure.name}:"]
foreground, background = None, None
for field in instance._type.fields:
for field in instance._type.lookup.values():
if getattr(field.type, "anonymous", False):
continue

if color:
foreground, background = colors[ci % len(colors)]
palette.append((instance._size(field.name), background))
Expand Down
36 changes: 36 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,42 @@ def test_dumpstruct(capsys, compiled):
uint32 testval;
};
"""

cs = cstruct.cstruct()
cs.load(cdef, compiled=compiled)

assert verify_compiled(cs.test, compiled)

buf = b"\x39\x05\x00\x00"
obj = cs.test(buf)

dumpstruct(cs.test, buf)
captured_1 = capsys.readouterr()

dumpstruct(obj)
captured_2 = capsys.readouterr()

assert captured_1.out == captured_2.out

out_1 = dumpstruct(cs.test, buf, output="string")
out_2 = dumpstruct(obj, output="string")

assert out_1 == out_2

with pytest.raises(ValueError) as excinfo:
dumpstruct(obj, output="generator")
assert str(excinfo.value) == "Invalid output argument: 'generator' (should be 'print' or 'string')."


def test_dumpstruct_anonymous(capsys, compiled):
cdef = """
struct test {
struct {
uint32 testval;
};
};
"""

cs = cstruct.cstruct()
cs.load(cdef, compiled=compiled)

Expand Down