diff --git a/Tests/images/timeout-d675703545fee17acab56e5fec644c19979175de.eps b/Tests/images/timeout-d675703545fee17acab56e5fec644c19979175de.eps new file mode 100644 index 00000000000..5000ca9aa55 Binary files /dev/null and b/Tests/images/timeout-d675703545fee17acab56e5fec644c19979175de.eps differ diff --git a/Tests/test_file_eps.py b/Tests/test_file_eps.py index ea91375dadb..e616ffa0cf6 100644 --- a/Tests/test_file_eps.py +++ b/Tests/test_file_eps.py @@ -1,5 +1,4 @@ import io - import pytest from PIL import EpsImagePlugin, Image, features @@ -264,3 +263,17 @@ def test_emptyline(): assert image.mode == "RGB" assert image.size == (460, 352) assert image.format == "EPS" + + +@pytest.mark.timeout(timeout=5) +@pytest.mark.parametrize( + "test_file", + [ + ("Tests/images/timeout-d675703545fee17acab56e5fec644c19979175de.eps") + ], +) +def test_timeout(test_file): + with open(test_file, "rb") as f: + with pytest.raises(Image.UnidentifiedImageError): + with Image.open(f): + pass diff --git a/src/PIL/EpsImagePlugin.py b/src/PIL/EpsImagePlugin.py index dc61f48edc9..3bf8ee0ab35 100644 --- a/src/PIL/EpsImagePlugin.py +++ b/src/PIL/EpsImagePlugin.py @@ -170,12 +170,12 @@ def seek(self, offset, whence=io.SEEK_SET): self.fp.seek(offset, whence) def readline(self): - s = self.char or b"" + s = [self.char or b""] self.char = None c = self.fp.read(1) - while c not in b"\r\n": - s = s + c + while (c not in b"\r\n") and len(c): + s.append(c) c = self.fp.read(1) self.char = self.fp.read(1) @@ -183,7 +183,7 @@ def readline(self): if self.char in b"\r\n": self.char = None - return s.decode("latin-1") + return b"".join(s).decode("latin-1") def _accept(prefix):