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

Indefinite loop while counting frames in truncated tiff image #5472

Closed
bauerji opened this issue May 7, 2021 · 8 comments · Fixed by #5473
Closed

Indefinite loop while counting frames in truncated tiff image #5472

bauerji opened this issue May 7, 2021 · 8 comments · Fixed by #5473
Labels

Comments

@bauerji
Copy link

bauerji commented May 7, 2021

What did you do?

I tried to count number of frames in a tiff image. However, the image is corrupted.

What did you expect to happen?

I expected some kind of exception (OSError).

What actually happened?

Warning is logged
.../lib/python3.9/site-packages/PIL/TiffImagePlugin.py:793: UserWarning: Truncated File Read
and code is trapped inside indefinite for loop.

What are your OS, Python and Pillow versions?

  • OS: MacOS Big Sur
  • Python: 3.9.4
  • Pillow: 8.2.0

also occurs in docker container with

  • OS: Ubuntu focal
  • Python: 3.9.0
  • Pillow: 8.2.0
from pathlib import Path
from PIL import Image

# path to the attached image
file_path = Path("truncated_img.tiff")

Image.open(file_path).n_frames

truncated_img.tiff.zip

@radarhere
Copy link
Member

Thanks for reporting. Your file is telling Pillow that the next frame is in the same position as the current frame, leading to an infinite loop.

I've created PR #5473 to resolve this - although my change doesn't raise an error like you are expecting, it just concludes that your image only has 1 frame.

@radarhere radarhere added the TIFF label May 7, 2021
@bauerji
Copy link
Author

bauerji commented May 7, 2021

Thank you for such a quick solution. This seems to work when the last frame is corrupted. But when the image contains e.g. 4 frames and the third one is corrupted, n_frames returns 2 which is incorrect.

@radarhere
Copy link
Member

radarhere commented May 7, 2021

Would you be able to attach that image? The information that is incorrect is the information telling us where the next image is, so I'm not sure if there is a way to figure that out without it. The specification states that "This file offset may
point anywhere in the file, even after the image data."
If I create a multiframe image like you're describing and check to see if commercial software can handle it, I can't open it in Preview or GIMP.

@bauerji
Copy link
Author

bauerji commented May 7, 2021

Unfortunately I cannot shared this file, it is confidential. As I tried to create another one, I also wasn't able to open it in such a software. That leads me to outcome that n_frames should really raise an exception rather than return an invalid number. It should be sufficient to add raise in here https://github.com/python-pillow/Pillow/blob/master/src/PIL/TiffImagePlugin.py#L794 (this change would cause 2 tests to fail)

@radarhere
Copy link
Member

Just to be clear, my change doesn't affect the UserWarning that you're seeing for your image.

@bauerji
Copy link
Author

bauerji commented May 10, 2021

Okay, your solution fits fine to our needs. Thank you!

@radarhere
Copy link
Member

It's not clear to me - are you still interested in n_frames throwing an error? I've been mulling it over.

@bauerji
Copy link
Author

bauerji commented May 10, 2021

I am sorry for not being clear enough. I like the solution just with UserWarning. Throwing an error is not necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants