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

Raise UnidentifiedImageError when opening TIFF without dimensions #8535

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

radarhere
Copy link
Member

Resolves #8530

#8319 changed

xsize = int(self.tag_v2.get(IMAGEWIDTH))
ysize = int(self.tag_v2.get(IMAGELENGTH))

to

xsize = self.tag_v2.get(IMAGEWIDTH)
ysize = self.tag_v2.get(IMAGELENGTH)
if not isinstance(xsize, int) or not isinstance(ysize, int):
msg = "Invalid dimensions"
raise ValueError(msg)

This change seems reasonable, especially when you consider #4103.

However, if IMAGEWIDTH or IMAGELENGTH were missing, then int(None) previously raised a TypeError. TypeErrors are caught when opening images

Pillow/src/PIL/Image.py

Lines 3497 to 3506 in 82199ef

factory, accept = OPEN[i]
result = not accept or accept(prefix)
if isinstance(result, str):
warning_messages.append(result)
elif result:
fp.seek(0)
im = factory(fp, filename)
_decompression_bomb_check(im.size)
return im
except (SyntaxError, IndexError, TypeError, struct.error) as e:

but the ValueErrors are not.

So this change inadvertently broke feeding a TIFF image to ImageFile.Parser, which relies on OSError (which UnidentifiedImageErrors are) to know that there isn't enough data yet.

Pillow/src/PIL/ImageFile.py

Lines 468 to 473 in 82199ef

# attempt to open this file
try:
with io.BytesIO(self.data) as fp:
im = Image.open(fp)
except OSError:
pass # not enough data

This PR changes the code to raise a TypeError again.

ysize = self.tag_v2[IMAGELENGTH]
except KeyError as e:
msg = "Missing dimensions"
raise TypeError(msg) from e
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also a new error type that was not raised in past versions, isn't it? So i will also make trouble in Django...

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's a good idea to always raise a special error for all parsers, in all cases where the chunk data will not be enough to get the meta data and use RuntimeError as a base, because it's already catch?

Copy link
Member Author

@radarhere radarhere Nov 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 10.4.0, a TypeError would have been raised here by int(self.tag_v2.get(IMAGEWIDTH)) being run when there was no IMAGEWIDTH tag - effectively, int(None).

This is raising a TypeError again.

It is then caught in Image.open, and if no other formats think they can interpret the file, an UnidentifiedImageError is raised, which inherits from OSError, and is then caught in Parser.feed(), and the Django loop continues, unaware of the error.

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 this pull request may close these issues.

Pillow 11.0.0 and Django: TiffImagePlugin raised 'Invalid dimensions' error
2 participants