diff --git a/Tests/images/no_icons.ico b/Tests/images/no_icons.ico new file mode 100644 index 000000000..4012786aa Binary files /dev/null and b/Tests/images/no_icons.ico differ 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'])