Merge pull request #4528 from radarhere/png_seek

Raise an EOFError when seeking too far in PNG
This commit is contained in:
Hugo van Kemenade 2020-04-06 14:30:04 +03:00 committed by GitHub
commit e634c4dc0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 14 deletions

View File

@ -637,6 +637,9 @@ class TestFilePng:
with Image.open(TEST_PNG_FILE) as im: with Image.open(TEST_PNG_FILE) as im:
im.seek(0) im.seek(0)
with pytest.raises(EOFError):
im.seek(1)
@pytest.mark.skipif(is_win32(), reason="Requires Unix or macOS") @pytest.mark.skipif(is_win32(), reason="Requires Unix or macOS")
@skip_unless_feature("zlib") @skip_unless_feature("zlib")

View File

@ -229,7 +229,7 @@ Plugin reference
---------------------------- ----------------------------
.. automodule:: PIL.PngImagePlugin .. automodule:: PIL.PngImagePlugin
:members: ChunkStream, PngImageFile, PngStream, getchunks, is_cid, putchunk :members: ChunkStream, PngStream, getchunks, is_cid, putchunk
:show-inheritance: :show-inheritance:
.. autoclass:: PIL.PngImagePlugin.ChunkStream .. autoclass:: PIL.PngImagePlugin.ChunkStream
:members: :members:

View File

@ -673,7 +673,7 @@ class PngImageFile(ImageFile.ImageFile):
self._text = None self._text = None
self.tile = self.png.im_tile self.tile = self.png.im_tile
self.custom_mimetype = self.png.im_custom_mimetype self.custom_mimetype = self.png.im_custom_mimetype
self._n_frames = self.png.im_n_frames self.n_frames = self.png.im_n_frames or 1
self.default_image = self.info.get("default_image", False) self.default_image = self.info.get("default_image", False)
if self.png.im_palette: if self.png.im_palette:
@ -685,15 +685,16 @@ class PngImageFile(ImageFile.ImageFile):
else: else:
self.__prepare_idat = length # used by load_prepare() self.__prepare_idat = length # used by load_prepare()
if self._n_frames is not None: if self.png.im_n_frames is not None:
self._close_exclusive_fp_after_loading = False self._close_exclusive_fp_after_loading = False
self.png.save_rewind() self.png.save_rewind()
self.__rewind_idat = self.__prepare_idat self.__rewind_idat = self.__prepare_idat
self.__rewind = self.__fp.tell() self.__rewind = self.__fp.tell()
if self.default_image: if self.default_image:
# IDAT chunk contains default image and not first animation frame # IDAT chunk contains default image and not first animation frame
self._n_frames += 1 self.n_frames += 1
self._seek(0) self._seek(0)
self.is_animated = self.n_frames > 1
@property @property
def text(self): def text(self):
@ -710,16 +711,6 @@ class PngImageFile(ImageFile.ImageFile):
self.seek(frame) self.seek(frame)
return self._text return self._text
@property
def n_frames(self):
if self._n_frames is None:
return 1
return self._n_frames
@property
def is_animated(self):
return self._n_frames is not None and self._n_frames > 1
def verify(self): def verify(self):
"""Verify PNG file""" """Verify PNG file"""