From b7055aeaba454ef94fa304757491cac13abd3913 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Wed, 22 Feb 2023 10:36:10 +0200 Subject: [PATCH] IcoImagePlugin: raise specific exception like CurImagePlugin if no images --- Tests/images/no_icons.ico | Bin 0 -> 6 bytes Tests/test_file_ico.py | 5 +++++ src/PIL/IcoImagePlugin.py | 4 ++++ 3 files changed, 9 insertions(+) create mode 100644 Tests/images/no_icons.ico diff --git a/Tests/images/no_icons.ico b/Tests/images/no_icons.ico new file mode 100644 index 0000000000000000000000000000000000000000..4012786aaf7d31ccf7f963c14aae96e0b80b9f39 GIT binary patch literal 6 NcmZQzU}Rum0000A00IC2 literal 0 HcmV?d00001 diff --git a/Tests/test_file_ico.py b/Tests/test_file_ico.py index 9c1c3cf17..6c7303b6e 100644 --- a/Tests/test_file_ico.py +++ b/Tests/test_file_ico.py @@ -229,3 +229,8 @@ def test_draw_reloaded(tmp_path): with Image.open(outfile) as im: assert_image_equal_tofile(im, "Tests/images/hopper_draw.ico") + + +def test_empty(): + with pytest.raises(SyntaxError, match="No images were found"): + IcoImagePlugin.IcoImageFile("Tests/images/no_icons.ico") diff --git a/src/PIL/IcoImagePlugin.py b/src/PIL/IcoImagePlugin.py index a188f8fdc..5de561782 100644 --- a/src/PIL/IcoImagePlugin.py +++ b/src/PIL/IcoImagePlugin.py @@ -172,6 +172,10 @@ class IcoFile: self.entry.append(icon_header) + if not self.entry: + msg = "No images were found" + raise TypeError(msg) + self.entry = sorted(self.entry, key=lambda x: x["color_depth"]) # ICO images are usually squares # self.entry = sorted(self.entry, key=lambda x: x['width'])