Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Tests/test_file_jpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
import re
import struct
import warnings
from io import BytesIO
from pathlib import Path
Expand Down Expand Up @@ -766,6 +767,18 @@ def test_bad_mpo_header(self) -> None:

im.close()

def test_mp_entry_count_too_high(self) -> None:
"""Treat an MPO with fewer MP entries than images as JPEG"""
with open("Tests/images/sugarshack.mpo", "rb") as fp:
data = fp.read()

# Change the number of images to three, without adding a third MP entry
data = data[:6048] + struct.pack(">L", 3) + data[6052:]

with pytest.warns(UserWarning, match="malformed MPO file"):
with Image.open(BytesIO(data)) as im:
assert im.format == "JPEG"

@pytest.mark.parametrize("mode", ("1", "L", "RGB", "RGBX", "CMYK", "YCbCr"))
def test_save_correct_modes(self, mode: str) -> None:
out = BytesIO()
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/JpegImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ def _getmp(self: JpegImageFile) -> dict[int, Any] | None:
mpentry["Attribute"] = mpentryattr
mpentries.append(mpentry)
mp[0xB002] = mpentries
except KeyError as e:
except (KeyError, struct.error) as e:
msg = "malformed MP Index (bad MP Entry)"
raise SyntaxError(msg) from e
# Next we should try and parse the individual image unique ID list;
Expand Down
Loading