Raise UnidentifiedImageError when opening TIFF without dimensions by radarhere · Pull Request #8535 · python-pillow/Pillow
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
| 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.
| # 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.