Skip to content

Commit baae9ec

Browse files
authored
Merge pull request #5921 from radarhere/jpeg_eoi
Ensure JpegImagePlugin stops at the end of a truncated file
2 parents 525c840 + 1059eb5 commit baae9ec

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

Tests/test_file_jpeg.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,30 @@ def test_getxmp(self):
870870
with Image.open("Tests/images/hopper.jpg") as im:
871871
assert im.getxmp() == {}
872872

873+
@pytest.mark.timeout(timeout=1)
874+
def test_eof(self):
875+
# Even though this decoder never says that it is finished
876+
# the image should still end when there is no new data
877+
class InfiniteMockPyDecoder(ImageFile.PyDecoder):
878+
def decode(self, buffer):
879+
return 0, 0
880+
881+
decoder = InfiniteMockPyDecoder(None)
882+
883+
def closure(mode, *args):
884+
decoder.__init__(mode, *args)
885+
return decoder
886+
887+
Image.register_decoder("INFINITE", closure)
888+
889+
with Image.open(TEST_FILE) as im:
890+
im.tile = [
891+
("INFINITE", (0, 0, 128, 128), 0, ("RGB", 0, 1)),
892+
]
893+
ImageFile.LOAD_TRUNCATED_IMAGES = True
894+
im.load()
895+
ImageFile.LOAD_TRUNCATED_IMAGES = False
896+
873897

874898
@pytest.mark.skipif(not is_win32(), reason="Windows only")
875899
@skip_unless_feature("jpg")

src/PIL/JpegImagePlugin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,10 @@ def load_read(self, read_bytes):
401401
"""
402402
s = self.fp.read(read_bytes)
403403

404-
if not s and ImageFile.LOAD_TRUNCATED_IMAGES:
404+
if not s and ImageFile.LOAD_TRUNCATED_IMAGES and not hasattr(self, "_ended"):
405405
# Premature EOF.
406406
# Pretend file is finished adding EOI marker
407+
self._ended = True
407408
return b"\xFF\xD9"
408409

409410
return s

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy