From 6bd3ed439a2f7f542df52c39211ae37a48b75238 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 4 Aug 2023 23:54:05 +1000 Subject: [PATCH] Ignore IPTC field that is only zero bytes --- Tests/test_file_iptc.py | 13 +++++++++++++ src/PIL/IptcImagePlugin.py | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Tests/test_file_iptc.py b/Tests/test_file_iptc.py index 2d99528d37c..1e5f2d32551 100644 --- a/Tests/test_file_iptc.py +++ b/Tests/test_file_iptc.py @@ -30,6 +30,19 @@ def test_getiptcinfo_jpg_found(): assert iptc[(2, 101)] == b"Hungary" +def test_getiptcinfo_zero_padding(): + # Arrange + with Image.open(TEST_FILE) as im: + im.info["photoshop"][0x0404] += b"\x00\x00\x00" + + # Act + iptc = IptcImagePlugin.getiptcinfo(im) + + # Assert + assert isinstance(iptc, dict) + assert len(iptc) == 3 + + def test_getiptcinfo_tiff_none(): # Arrange with Image.open("Tests/images/hopper.tif") as im: diff --git a/src/PIL/IptcImagePlugin.py b/src/PIL/IptcImagePlugin.py index 6ce4975c09d..1616395dec2 100644 --- a/src/PIL/IptcImagePlugin.py +++ b/src/PIL/IptcImagePlugin.py @@ -58,7 +58,7 @@ def field(self): # # get a IPTC field header s = self.fp.read(5) - if not len(s): + if not s.strip(b"\x00"): return None, 0 tag = s[1], s[2]