dissect.cstruct
dissect.cstruct copied to clipboard
Errors when initialising a struct with partial keywords or arguments
When initialising a struct with partial keywords or arguments the other fields become None
resulting in exceptions, for example when printing the instance of the struct.
When initialising without any arguments it works and all values are 0 as expected. But when you only initialise some of the fields it breaks, example:
from dissect.cstruct import cstruct
C_DEF = """"
struct FooBar {
uint32 foo;
uint32 bar;
};
"""
cs = cstruct().load(C_DEF)
print(cs.FooBar()) # works as expected: <FooBar foo=0x0 bar=0x0>
print(cs.FooBar(1, 1)) # works as expected: <FooBar foo=0x1 bar=0x1>
print(cs.FooBar(1)) # breaks, bar=None
print(cs.FooBar(bar=1)) # breaks, foo=None
The exception:
$ python3 kwargs_init.py
<FooBar foo=0x0 bar=0x0>
<FooBar foo=0x1 bar=0x1>
Traceback (most recent call last):
File "/workspaces/dissect.cstruct/examples/kwargs_init.py", line 14, in <module>
print(cs.FooBar(1)) # breaks, bar=None
^^^^^^^^^^^^^^^^^^^
File "/workspaces/dissect.cstruct/dissect/cstruct/types/structure.py", line 388, in __repr__
value = hex(value)
^^^^^^^^^^
TypeError: 'NoneType' object cannot be interpreted as an integer