Skip to content

Commit b844007

Browse files
authored
Clear TIFF core image if memory mapping was used for last load (#8962)
2 parents 6bd5568 + 0eef5e2 commit b844007

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

Tests/test_tiff_crashes.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,17 @@ def test_tiff_crashes(test_file: str) -> None:
5252
pytest.skip("test image not found")
5353
except OSError:
5454
pass
55+
56+
57+
def test_tiff_mmap() -> None:
58+
try:
59+
with Image.open("Tests/images/crash_mmap.tif") as im:
60+
im.seek(1)
61+
im.load()
62+
63+
im.seek(0)
64+
im.load()
65+
except FileNotFoundError:
66+
if on_ci():
67+
raise
68+
pytest.skip("test image not found")

src/PIL/TiffImagePlugin.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,9 +1217,10 @@ def seek(self, frame: int) -> None:
12171217
return
12181218
self._seek(frame)
12191219
if self._im is not None and (
1220-
self.im.size != self._tile_size or self.im.mode != self.mode
1220+
self.im.size != self._tile_size
1221+
or self.im.mode != self.mode
1222+
or self.readonly
12211223
):
1222-
# The core image will no longer be used
12231224
self._im = None
12241225

12251226
def _seek(self, frame: int) -> None:

src/map.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ PyImaging_MapBuffer(PyObject *self, PyObject *args) {
137137
}
138138
}
139139

140+
im->read_only = view.readonly;
140141
im->destroy = mapping_destroy_buffer;
141142

142143
Py_INCREF(target);

0 commit comments

Comments
 (0)