From fb8cdbfeead33d1fafd052da6b9593f14505d16d Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 21 Sep 2026 19:23:06 +1000 Subject: [PATCH] Only replace a maximum of two Exif headers --- Tests/test_image.py | 6 ++++++ src/PIL/Image.py | 3 +-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index 98ce4ca2657..f4e2b341687 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -801,6 +801,12 @@ def test_duplicate_exif_header(self) -> None: exif = im.getexif() assert exif[274] == 1 + with Image.open("Tests/images/exif.png") as im: + im.load() + im.info["exif"] = b"Exif\x00\x00" * 2 + im.info["exif"] + with pytest.raises(SyntaxError): + im.getexif() + def test_empty_get_ifd(self) -> None: exif = Image.Exif() ifd = exif.get_ifd(0x8769) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 4bb7c51aaad..bde53152bbb 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -4143,8 +4143,7 @@ def load(self, data: bytes) -> None: self._data.clear() self._hidden_data.clear() self._ifds.clear() - while data and data.startswith(b"Exif\x00\x00"): - data = data[6:] + data = re.sub(b"^(Exif\x00\x00){1,2}", b"", data) if not data: self._info = None return