Do not allow negative offset with memory mapping

This commit is contained in:
Andrew Murray 2025-07-26 21:58:41 +10:00
parent 8b41e5b479
commit 6d19b8adef
2 changed files with 8 additions and 0 deletions

View File

@ -164,6 +164,11 @@ class TestImageFile:
with pytest.raises(OSError): with pytest.raises(OSError):
p.close() p.close()
def test_negative_offset(self) -> None:
with Image.open("Tests/images/raw_negative_stride.bin") as im:
with pytest.raises(ValueError, match="Tile offset cannot be negative"):
im.load()
def test_no_format(self) -> None: def test_no_format(self) -> None:
buf = BytesIO(b"\x00" * 255) buf = BytesIO(b"\x00" * 255)

View File

@ -313,6 +313,9 @@ class ImageFile(Image.Image):
and args[0] == self.mode and args[0] == self.mode
and args[0] in Image._MAPMODES and args[0] in Image._MAPMODES
): ):
if offset < 0:
msg = "Tile offset cannot be negative"
raise ValueError(msg)
try: try:
# use mmap, if possible # use mmap, if possible
import mmap import mmap