Skip to content

Commit 6d19b8a

Browse files
committed
Do not allow negative offset with memory mapping
1 parent 8b41e5b commit 6d19b8a

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

Tests/test_imagefile.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ def test_negative_stride(self) -> None:
164164
with pytest.raises(OSError):
165165
p.close()
166166

167+
def test_negative_offset(self) -> None:
168+
with Image.open("Tests/images/raw_negative_stride.bin") as im:
169+
with pytest.raises(ValueError, match="Tile offset cannot be negative"):
170+
im.load()
171+
167172
def test_no_format(self) -> None:
168173
buf = BytesIO(b"\x00" * 255)
169174

src/PIL/ImageFile.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,9 @@ def load(self) -> Image.core.PixelAccess | None:
313313
and args[0] == self.mode
314314
and args[0] in Image._MAPMODES
315315
):
316+
if offset < 0:
317+
msg = "Tile offset cannot be negative"
318+
raise ValueError(msg)
316319
try:
317320
# use mmap, if possible
318321
import mmap

0 commit comments

Comments
 (0)