Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not allow TIFF to seek to a past frame #5473

Merged
merged 1 commit into from Jun 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added Tests/images/multipage_multiple_frame_loop.tiff
Binary file not shown.
Binary file added Tests/images/multipage_out_of_order.tiff
Binary file not shown.
Binary file added Tests/images/multipage_single_frame_loop.tiff
Binary file not shown.
13 changes: 13 additions & 0 deletions Tests/test_file_tiff.py
Expand Up @@ -301,6 +301,19 @@ def test_multipage_last_frame(self):
assert im.size == (20, 20)
assert im.convert("RGB").getpixel((0, 0)) == (0, 0, 255)

def test_frame_order(self):
# A frame can't progress to itself after reading
with Image.open("Tests/images/multipage_single_frame_loop.tiff") as im:
assert im.n_frames == 1

# A frame can't progress to a frame that has already been read
with Image.open("Tests/images/multipage_multiple_frame_loop.tiff") as im:
assert im.n_frames == 2

# Frames don't have to be in sequence
with Image.open("Tests/images/multipage_out_of_order.tiff") as im:
assert im.n_frames == 3

def test___str__(self):
filename = "Tests/images/pil136.tiff"
with Image.open(filename) as im:
Expand Down
7 changes: 6 additions & 1 deletion src/PIL/TiffImagePlugin.py
Expand Up @@ -1067,7 +1067,12 @@ def _seek(self, frame):
self._frame_pos.append(self.__next)
logger.debug("Loading tags, location: %s" % self.fp.tell())
self.tag_v2.load(self.fp)
self.__next = self.tag_v2.next
if self.tag_v2.next in self._frame_pos:
# This IFD has already been processed
# Declare this to be the end of the image
self.__next = 0
else:
self.__next = self.tag_v2.next
if self.__next == 0:
self._n_frames = frame + 1
if len(self._frame_pos) == 1:
Expand Down