From 1db7de771e7352fda3f33953e082237ed7380b60 Mon Sep 17 00:00:00 2001 From: Jennifer Taylor Date: Mon, 25 Oct 2021 21:19:59 +0000 Subject: [PATCH] Fix returning too much data when grabbing a chunk that overlaps a patched section and the end of a file. --- arcadeutils/filebytes.py | 16 +++++++++------- tests/test_FileBytes.py | 12 ++++++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/arcadeutils/filebytes.py b/arcadeutils/filebytes.py index d759d62..56fc561 100644 --- a/arcadeutils/filebytes.py +++ b/arcadeutils/filebytes.py @@ -286,7 +286,7 @@ class FileBytes: # Determine start of slice if key.start is None: - start = 0 if step > 0 else self.__patchlength + start = 0 if step > 0 else (self.__patchlength - 1) elif key.start < 0: start = self.__patchlength + key.start else: @@ -407,7 +407,7 @@ class FileBytes: # Append any amount of data we need to read past the end of the file. if len(data) < stop - start: - data.extend([0] * (stop - len(data))) + data.extend([0] * ((stop - start) - len(data))) else: data = [0] * (stop - start) @@ -418,23 +418,25 @@ class FileBytes: return bytes(data) elif start > stop and step == -1: + start += 1 + stop += 1 if not modifications: # This is just a continguous read, reversed - self.__handle.seek(stop + 1) + self.__handle.seek(stop) return self.__handle.read(start - stop)[::-1] else: - if (stop + 1) < self.__filelength: - self.__handle.seek(stop + 1) + if stop < self.__filelength: + self.__handle.seek(stop) data = [x for x in self.__handle.read(start - stop)] # Append any amount of data we need to read past the end of the file. if len(data) < start - stop: - data.extend([0] * (start - len(data))) + data.extend([0] * ((start - stop) - len(data))) else: data = [0] * (start - stop) # Now we have to modify the data with our own overlay. - for index, off in enumerate(range(stop + 1, start + 1)): + for index, off in enumerate(range(stop, start)): if off in self.__patches: data[index] = self.__patches[off] diff --git a/tests/test_FileBytes.py b/tests/test_FileBytes.py index 49e3154..09f83b1 100644 --- a/tests/test_FileBytes.py +++ b/tests/test_FileBytes.py @@ -298,6 +298,18 @@ class TestFileBytes(unittest.TestCase): b, ) + def test_read_after_append(self) -> None: + fb = FileBytes(io.BytesIO(b"0123456789")) + + # Do some simple appending. + fb.append(b"abcdef") + + # Now try to read over the boundary. + self.assertEqual( + fb[5:15], + b"56789abcde", + ) + def test_modify_variants(self) -> None: fb = FileBytes(io.BytesIO(b"0123456789"))