From 7319d8632824dbfcef2769c474fa964d237c3492 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 12 Oct 2023 10:13:46 +1100 Subject: [PATCH] Catch struct.error from truncated EXIF when reading DPI --- Tests/images/truncated_exif_dpi.jpg | Bin 0 -> 7674 bytes Tests/test_file_jpeg.py | 7 +++++++ src/PIL/JpegImagePlugin.py | 14 +++++++++++--- 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 Tests/images/truncated_exif_dpi.jpg diff --git a/Tests/images/truncated_exif_dpi.jpg b/Tests/images/truncated_exif_dpi.jpg new file mode 100644 index 0000000000000000000000000000000000000000..b41ab40044221dbf135e8809bb5202c898c74153 GIT binary patch literal 7674 zcmeHJdu&rx82@hXx$Rz5#$$t_aCrFQy1cG&~0k&@e;=1OtMIkLCKE+c#N+_(%IsPuH_wzw@2% zJHOv|&OKK?EuV+JJZh{D9mI4-~hFjR2LEFc#2o4f0Jkr^o5GIbD3-cpc>H zjg^Gp5H?$6h z#o>|6rF#C!S+jXf7pPz`Btr%yTdZzZp~+N1V^Uuwc0_mkB;aZRj*shoJi__zAIS}Z z+X}!W;DEEiZEZo^2EeVfx+D*PhVsgv?{QK57~%mEE)cPa;wB$%pt#{|nR;M`KdAxNT?r&^dKXQOBrlmTCNkKr)S|AH-cFLGAe zFzi#@&DGQJe-?6I-e4)BcogF8R!ecUA0KHI>#F?tmdm3zBOVQ)8su%REkul4Lp8Ng zDyc<`aa1;&w=96yEVR|kM0-E9w>LYd(Ya}e4>{_qrX!|t&Rg9E)D=bmuxguNr1c(( zI7@PxsSmD)Eol&niV5n`||M6j3A zyfpq5m&fGKOHv&6Dw+%9kX*s7@WnVJ#bc`}Lp$arvprZ)-(0e|Q7kd~{ok-jWzrcUgMy=$YKSqpsTQfY)Q=mWjJ;3J&N29?Vb*di?JK z2}c@11Up&;92L+a;IDwsxCkGUGVb#ac<|7q%)Ep%Ma*Crx(&QI-v(DPo-G)O2Lt}{ zqOA#fkK|uREo?rt8#g(rwlK!mej`v7fTL z**)wxz_WYV4)$|)Kl>j0anQf_n)~)nu;vO6YNIUZehlypR}}ZBR<9kR9jPtS4%238 zD}r9UHeH*eEz^#{ks-nAgmyC@p@RssP_uwqz2~AQLkvbjt(iVpXEBs9;(XNNiba@@ zF(p)=?lT80K;Q6l=4V8Pk{n{ZYggyfy&jD1I}d?+1eA#6AgGJgMe0J})nnBK>Kt`B z#ews!9)n{A*yB^nBedb&0$^~u7D!@~&BN#Fbd&f(yy^tr=&+8@<}G$RuMD|)NpK5N ztI#kW=sij20o*hz_aBpXIArrw0!;apKKDYl*@-=A+j~<<(2e5|h%>2Mx~196cuM;jt4QnK&uG;88J zvAnvbwr(cUvo;8gO*V0^+v9DW-?m`kGs~ZSZpHI2tX#ML#g{g`yz!M+H*eXx?ajB| ze&^kt?|-oC!;ji`@A=}(eP8WAaPaFx$Bv)qJbCJL*O~4MKV1Crr=KrfzH;NYo40?`tl*X#CN-T?&BG@<$N1G-= zqqGEnOHx`gZGD1G#vlyFz{zU->yxNS436D5kiP>0L*+A&!ZNs-*i?8Ndj8qGG7Dnx zW#uC=%zZS5xsSy#_wg9!J`uy*oiWUPGKRTN#V~jLd`J7a%F6Nc9evH?=R5kE$M5gx rYaYM9qpx}V{*J!p@%QuSYaV|;kG|&d_w(p$9)CZNzUHxfKkxb%>aiuO literal 0 HcmV?d00001 diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 769d7ed969a..a0822d0002c 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -767,6 +767,13 @@ def test_dpi_exif_string(self): # This should return the default assert im.info.get("dpi") == (72, 72) + def test_dpi_exif_truncated(self): + # Arrange + with Image.open("Tests/images/truncated_exif_dpi.jpg") as im: + # Act / Assert + # This should return the default + assert im.info.get("dpi") == (72, 72) + def test_no_dpi_in_exif(self): # Arrange # This is photoshop-200dpi.jpg with resolution removed from EXIF: diff --git a/src/PIL/JpegImagePlugin.py b/src/PIL/JpegImagePlugin.py index 2bb10e1f695..917bbf39fbb 100644 --- a/src/PIL/JpegImagePlugin.py +++ b/src/PIL/JpegImagePlugin.py @@ -170,11 +170,19 @@ def APP(self, marker): # 1 dpcm = 2.54 dpi dpi *= 2.54 self.info["dpi"] = dpi, dpi - except (TypeError, KeyError, SyntaxError, ValueError, ZeroDivisionError): - # SyntaxError for invalid/unreadable EXIF + except ( + struct.error, + KeyError, + SyntaxError, + TypeError, + ValueError, + ZeroDivisionError, + ): + # struct.error for truncated EXIF # KeyError for dpi not included - # ZeroDivisionError for invalid dpi rational value + # SyntaxError for invalid/unreadable EXIF # ValueError or TypeError for dpi being an invalid float + # ZeroDivisionError for invalid dpi rational value self.info["dpi"] = 72, 72