From 400ffbc18d43ea41b5d47c3dad3a2d7a4510ad56 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 10 Jan 2026 14:37:18 +1100 Subject: [PATCH] Raise EOFError when seeking too far --- Tests/test_file_psd.py | 5 +++++ src/PIL/PsdImagePlugin.py | 3 +++ 2 files changed, 8 insertions(+) diff --git a/Tests/test_file_psd.py b/Tests/test_file_psd.py index 8f2ca58a662..da572ae6338 100644 --- a/Tests/test_file_psd.py +++ b/Tests/test_file_psd.py @@ -85,6 +85,11 @@ def test_eoferror() -> None: # Test that seeking to the last frame does not raise an error im.seek(n_frames - 1) + # Test seeking past the last frame without calling n_frames first + with Image.open(test_file) as im: + with pytest.raises(EOFError): + im.seek(3) + def test_seek_tell() -> None: with Image.open(test_file) as im: diff --git a/src/PIL/PsdImagePlugin.py b/src/PIL/PsdImagePlugin.py index 69a8703dd8b..dd3d5ab95fd 100644 --- a/src/PIL/PsdImagePlugin.py +++ b/src/PIL/PsdImagePlugin.py @@ -175,6 +175,9 @@ def seek(self, layer: int) -> None: raise self._fp.ex # seek to given layer (1..max) + if layer > len(self.layers): + msg = "no more images in PSD file" + raise EOFError(msg) _, mode, _, tile = self.layers[layer - 1] self._mode = mode self.tile = tile