From 023dcf27c29e2b62c84b171d1473bdebd5fcf0eb Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 13 Jul 2019 08:37:17 +1000 Subject: [PATCH] Raise the same error if a truncated image is loaded a second time --- Tests/test_file_jpeg.py | 4 ++++ Tests/test_imagefile.py | 4 ++++ src/PIL/ImageFile.py | 1 - src/PIL/PngImagePlugin.py | 3 ++- 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 4ade11a2966..a9ba54dd1c1 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -372,6 +372,10 @@ def test_truncated_jpeg_throws_IOError(self): with self.assertRaises(IOError): im.load() + # Test that the error is raised if loaded a second time + with self.assertRaises(IOError): + im.load() + def _n_qtables_helper(self, n, test_file): im = Image.open(test_file) f = self.tempfile("temp.jpg") diff --git a/Tests/test_imagefile.py b/Tests/test_imagefile.py index 83170cb2ab5..87bba7c2f1e 100644 --- a/Tests/test_imagefile.py +++ b/Tests/test_imagefile.py @@ -113,6 +113,10 @@ def test_truncated_with_errors(self): with self.assertRaises(IOError): im.load() + # Test that the error is raised if loaded a second time + with self.assertRaises(IOError): + im.load() + def test_truncated_without_errors(self): if "zip_encoder" not in codecs: self.skipTest("PNG (zlib) encoder not available") diff --git a/src/PIL/ImageFile.py b/src/PIL/ImageFile.py index e5173a1fb75..49d8ef97ceb 100644 --- a/src/PIL/ImageFile.py +++ b/src/PIL/ImageFile.py @@ -243,7 +243,6 @@ def load(self): if LOAD_TRUNCATED_IMAGES: break else: - self.tile = [] raise IOError( "image file is truncated " "(%d bytes not processed)" % len(b) diff --git a/src/PIL/PngImagePlugin.py b/src/PIL/PngImagePlugin.py index 10e18e4a071..f69ca046633 100644 --- a/src/PIL/PngImagePlugin.py +++ b/src/PIL/PngImagePlugin.py @@ -612,7 +612,7 @@ def _open(self): rawmode, data = self.png.im_palette self.palette = ImagePalette.raw(rawmode, data) - self.__idat = length # used by load_read() + self.__prepare_idat = length # used by load_prepare() @property def text(self): @@ -645,6 +645,7 @@ def load_prepare(self): if self.info.get("interlace"): self.decoderconfig = self.decoderconfig + (1,) + self.__idat = self.__prepare_idat # used by load_read() ImageFile.ImageFile.load_prepare(self) def load_read(self, read_bytes):