pfp icon indicating copy to clipboard operation
pfp copied to clipboard

wrong _pfp__offset for primitive array items

Open michaelmera opened this issue 3 years ago • 1 comments

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 avatar Aug 10 '21 13:08 michaelmera

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 avatar Aug 10 '21 13:08 michaelmera