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 wrong _pfp__offset for numeric array items #140

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions pfp/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2313,6 +2313,8 @@ def __getitem__(self, idx):
res._pfp__parent = self
res._pfp__array_idx = idx
res._pfp__name = "{}[{}]".format(self._pfp__name, idx)
if self._pfp__offset >= 0:
res._pfp__offset = self._pfp__offset + offset
return res

def __setitem__(self, idx, value):
Expand Down
18 changes: 18 additions & 0 deletions tests/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,24 @@ def test_array_with_root_scope(self):
stdout="00|00|00|00|",
)

def test_numerical_array_offset(self):
dom = self._test_parse_build(
"\xFF\x00\x00\x11\x11\x22\x22",
"""
char shift_by_one;
uint16 the_array[3];
"""
)

self.assertEqual(dom.the_array[0]._pfp__offset, 1)
self.assertEqual(dom.the_array[1]._pfp__offset, 3)
self.assertEqual(dom.the_array[2]._pfp__offset, 5)

adhoc_array = Array(3, dom.the_array.field_cls)
self.assertEqual(adhoc_array[0]._pfp__offset, -1)
self.assertEqual(adhoc_array[1]._pfp__offset, -1)
self.assertEqual(adhoc_array[2]._pfp__offset, -1)


if __name__ == "__main__":
unittest.main()