Clear TIFF core image if memory mapping was used for last load (#8962)

This commit is contained in:
Andrew Murray 2025-06-10 17:03:31 +10:00 committed by GitHub
commit b844007cdc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 2 deletions

View File

@ -52,3 +52,17 @@ def test_tiff_crashes(test_file: str) -> None:
pytest.skip("test image not found") pytest.skip("test image not found")
except OSError: except OSError:
pass pass
def test_tiff_mmap() -> None:
try:
with Image.open("Tests/images/crash_mmap.tif") as im:
im.seek(1)
im.load()
im.seek(0)
im.load()
except FileNotFoundError:
if on_ci():
raise
pytest.skip("test image not found")

View File

@ -1217,9 +1217,10 @@ class TiffImageFile(ImageFile.ImageFile):
return return
self._seek(frame) self._seek(frame)
if self._im is not None and ( if self._im is not None and (
self.im.size != self._tile_size or self.im.mode != self.mode self.im.size != self._tile_size
or self.im.mode != self.mode
or self.readonly
): ):
# The core image will no longer be used
self._im = None self._im = None
def _seek(self, frame: int) -> None: def _seek(self, frame: int) -> None:

View File

@ -137,6 +137,7 @@ PyImaging_MapBuffer(PyObject *self, PyObject *args) {
} }
} }
im->read_only = view.readonly;
im->destroy = mapping_destroy_buffer; im->destroy = mapping_destroy_buffer;
Py_INCREF(target); Py_INCREF(target);