Do not allow negative offset with memory mapping (#9235)

This commit is contained in:
Andrew Murray 2025-10-08 19:02:27 +11:00 committed by GitHub
commit cec262ce6b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 0 deletions

View File

@ -164,6 +164,11 @@ class TestImageFile:
with pytest.raises(OSError):
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:
buf = BytesIO(b"\x00" * 255)

View File

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