Raise an error when EXIF data is too long

This commit is contained in:
Andrew Murray 2023-02-11 16:20:27 +11:00
parent 4ab4bfe89b
commit bb524018d3
2 changed files with 8 additions and 2 deletions

View File

@ -270,7 +270,10 @@ class TestFileJpeg:
# https://github.com/python-pillow/Pillow/issues/148
f = str(tmp_path / "temp.jpg")
im = hopper()
im.save(f, "JPEG", quality=90, exif=b"1" * 65532)
im.save(f, "JPEG", quality=90, exif=b"1" * 65533)
with pytest.raises(ValueError):
im.save(f, "JPEG", quality=90, exif=b"1" * 65534)
def test_exif_typeerror(self):
with Image.open("Tests/images/exif_typeerror.jpg") as im:

View File

@ -730,10 +730,10 @@ def _save(im, fp, filename):
extra = info.get("extra", b"")
MAX_BYTES_IN_MARKER = 65533
icc_profile = info.get("icc_profile")
if icc_profile:
ICC_OVERHEAD_LEN = 14
MAX_BYTES_IN_MARKER = 65533
MAX_DATA_BYTES_IN_MARKER = MAX_BYTES_IN_MARKER - ICC_OVERHEAD_LEN
markers = []
while icc_profile:
@ -764,6 +764,9 @@ def _save(im, fp, filename):
exif = info.get("exif", b"")
if isinstance(exif, Image.Exif):
exif = exif.tobytes()
if len(exif) > MAX_BYTES_IN_MARKER:
msg = "EXIF data is too long"
raise ValueError(msg)
# get keyword arguments
im.encoderconfig = (