Skip to content

Commit

Permalink
Fix the mapping stream add() function (#59)
Browse files Browse the repository at this point in the history
The sorting should be only on the runlist index. Blanket sorting on all the
tuple fields will fail as filehandles/BinaryIO is not sortable.
  • Loading branch information
pyrco authored Nov 14, 2024
1 parent 3bc67de commit 2a9439e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion dissect/util/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,11 @@ def add(self, offset: int, size: int, fh: BinaryIO, file_offset: int = 0) -> Non
size: The size that this mapped fh spans in the stream.
fh: The file-like object to map.
file_offset: The offset in the fh to start from.
Note that there is no check on overlapping offsets and/or sizes.
"""
self._runs.append((offset, size, fh, file_offset))
self._runs = sorted(self._runs)
self._runs = sorted(self._runs, key=lambda run: run[0])
self._buf = None
self.size = self._runs[-1][0] + self._runs[-1][1]

Expand Down
14 changes: 14 additions & 0 deletions tests/test_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,20 @@ def test_mapping_stream() -> None:
assert fh.read(1) == b""


def test_mapping_stream_same_offset() -> None:
buffers = [
io.BytesIO(b"\x01" * 512),
io.BytesIO(b"\x02" * 512),
]
fh = stream.MappingStream()
# Add them in different orders to test if sorting works
fh.add(0, 1024, buffers[0])
fh.add(0, 1024, buffers[1]) # This should not raise an exception in add()

assert fh._runs[0][2] == buffers[0]
assert fh._runs[1][2] == buffers[1]


def test_runlist_stream() -> None:
buf = io.BytesIO(b"\x01" * 512 + b"\x02" * 512 + b"\x03" * 512)
fh = stream.RunlistStream(buf, [(0, 32), (32, 16), (48, 48)], 1536, 16)
Expand Down

0 comments on commit 2a9439e

Please sign in to comment.