Skip to content
Merged
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
14 changes: 14 additions & 0 deletions Tests/test_file_tar.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,17 @@ def test_contextmanager() -> None:
with warnings.catch_warnings(action="error"):
with TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg"):
pass


@pytest.mark.parametrize("size", (b"-1", b"0"))
def test_odd(tmp_path: Path, size: bytes) -> None:
with open(TEST_TAR_FILE, "rb") as f:
data = bytearray(f.read())

data[124:135] = size.rjust(11)

tmpfile = tmp_path / "temp.tar"
tmpfile.write_bytes(data)

with pytest.raises(ValueError, match="offset must be positive"):
TarIO.TarIO(str(tmpfile), "test")
6 changes: 5 additions & 1 deletion src/PIL/TarIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ def __init__(self, tarfile: str, file: str) -> None:
if file == name:
break

self.fh.seek((size + 511) & (~511), io.SEEK_CUR)
offset = (size + 511) & ~511
if offset <= 0:
msg = "offset must be positive"
raise ValueError(msg)
self.fh.seek(offset, io.SEEK_CUR)

# Open region
super().__init__(self.fh, self.fh.tell(), size)