From cc632ca0fbf02a974ac9c5392f9c9cc4217bb846 Mon Sep 17 00:00:00 2001 From: Aleksandr Karpinskii Date: Fri, 23 Aug 2024 17:36:03 +0400 Subject: [PATCH] load truncated PNG --- src/PIL/PngImagePlugin.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/PIL/PngImagePlugin.py b/src/PIL/PngImagePlugin.py index 69047c3f137..a57939327a2 100644 --- a/src/PIL/PngImagePlugin.py +++ b/src/PIL/PngImagePlugin.py @@ -992,16 +992,21 @@ def load_end(self): length -= 4 try: ImageFile._safe_read(self.fp, length) - except OSError as e: + except OSError: if ImageFile.LOAD_TRUNCATED_IMAGES: break - else: - raise e + raise except AttributeError: logger.debug("%r %s %s (unknown)", cid, pos, length) - s = ImageFile._safe_read(self.fp, length) - if cid[1:2].islower(): - self.private_chunks.append((cid, s, True)) + try: + s = ImageFile._safe_read(self.fp, length) + except OSError: + if ImageFile.LOAD_TRUNCATED_IMAGES: + break + raise + else: + if cid[1:2].islower(): + self.private_chunks.append((cid, s, True)) self._text = self.png.im_text if not self.is_animated: self.png.close()