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

wrong _pfp__offset for primitive array items #139

Open
michaelmera opened this issue Aug 10, 2021 · 1 comment · May be fixed by #140
Open

wrong _pfp__offset for primitive array items #139

michaelmera opened this issue Aug 10, 2021 · 1 comment · May be fixed by #140
Assignees
Labels
bug needs-triage A fresh user-reported issue that needs triage (i.e. is not a concrete-issue)

Comments

@michaelmera
Copy link

Describe the bug

I found this problem while looking for template parts at specific offsets. All items inside a pfp.fields.Array containing Int/Float elements have a _pfp__offset of 0, instead of their actual positions in the parsed stream. It doesn't seems to be the case when the array items are structs.

To Reproduce

Here is a minimal test reproducing the problem:

    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)

It fails with:

Traceback (most recent call last):
  File "pfp/tests/test_arrays.py", line 346, in test_numerical_array_offset
    self.assertEqual(dom.the_array[0]._pfp__offset, 1)
  File ".local/share/pyenv/versions/3.7.11/lib/python3.7/unittest/case.py", line 852, in assertEqual
    assertion_func(first, second, msg=msg)
  File ".local/share/pyenv/versions/3.7.11/lib/python3.7/unittest/case.py", line 845, in _baseAssertEqual
    raise self.failureException(msg)
AssertionError: 0 != 1

Expected Behavior

The item's _pfp__offset should be set to the offset value of the item inside the parsed stream.

@michaelmera michaelmera added bug needs-triage A fresh user-reported issue that needs triage (i.e. is not a concrete-issue) labels Aug 10, 2021
@michaelmera
Copy link
Author

After investigation, arrays with elements deriving from NumericBase do not add their items to the internal DOM structure but they are created on-demand when requesting the item. The offset for the created item is not explicitly set, which explains the problem.

This seems pretty straightforward to fix. I'll submit a PR later today.

@michaelmera michaelmera linked a pull request Aug 10, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug needs-triage A fresh user-reported issue that needs triage (i.e. is not a concrete-issue)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants