diff --git a/Tests/images/timeout-d675703545fee17acab56e5fec644c19979175de.eps b/Tests/images/timeout-d675703545fee17acab56e5fec644c19979175de.eps new file mode 100644 index 000000000..5000ca9aa 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 3459310df..2f3a63bc6 100644 --- a/Tests/test_file_eps.py +++ b/Tests/test_file_eps.py @@ -1,6 +1,7 @@ import io from PIL import EpsImagePlugin, Image +import pytest from .helper import PillowTestCase, hopper, unittest @@ -252,3 +253,17 @@ class TestFileEps(PillowTestCase): self.assertEqual(image.mode, "RGB") self.assertEqual(image.size, (460, 352)) self.assertEqual(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 219f2dbb8..51cf47e2b 100644 --- a/src/PIL/EpsImagePlugin.py +++ b/src/PIL/EpsImagePlugin.py @@ -183,12 +183,12 @@ class PSFile(object): 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) @@ -196,7 +196,7 @@ class PSFile(object): 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):