Skip to content

Commit 6bffa3a

Browse files
authored
Only read until the offset of the next tile (#8609)
Co-authored-by: Andrew Murray <[email protected]>
1 parent 140e426 commit 6bffa3a

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

Tests/test_imagefile.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,26 @@ def test_safeblock(self) -> None:
131131

132132
assert_image_equal(im1, im2)
133133

134+
def test_tile_size(self) -> None:
135+
with open("Tests/images/hopper.tif", "rb") as im_fp:
136+
data = im_fp.read()
137+
138+
reads = []
139+
140+
class FP(BytesIO):
141+
def read(self, size: int | None = None) -> bytes:
142+
reads.append(size)
143+
return super().read(size)
144+
145+
fp = FP(data)
146+
with Image.open(fp) as im:
147+
assert len(im.tile) == 7
148+
149+
im.load()
150+
151+
# Despite multiple tiles, assert only one tile caused a read of maxblock size
152+
assert reads.count(im.decodermaxblock) == 1
153+
134154
def test_raise_oserror(self) -> None:
135155
with pytest.warns(DeprecationWarning):
136156
with pytest.raises(OSError):

src/PIL/ImageFile.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ def load(self) -> Image.core.PixelAccess | None:
345345
self.tile, lambda tile: (tile[0], tile[1], tile[3])
346346
)
347347
]
348-
for decoder_name, extents, offset, args in self.tile:
348+
for i, (decoder_name, extents, offset, args) in enumerate(self.tile):
349349
seek(offset)
350350
decoder = Image._getdecoder(
351351
self.mode, decoder_name, args, self.decoderconfig
@@ -358,8 +358,13 @@ def load(self) -> Image.core.PixelAccess | None:
358358
else:
359359
b = prefix
360360
while True:
361+
read_bytes = self.decodermaxblock
362+
if i + 1 < len(self.tile):
363+
next_offset = self.tile[i + 1].offset
364+
if next_offset > offset:
365+
read_bytes = next_offset - offset
361366
try:
362-
s = read(self.decodermaxblock)
367+
s = read(read_bytes)
363368
except (IndexError, struct.error) as e:
364369
# truncated png/gif
365370
if LOAD_TRUNCATED_IMAGES:

0 commit comments

Comments
 (0)