From 9ac511960780fff73c7b154c7db1c910520b2c20 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Sat, 22 May 2021 22:06:21 +0000 Subject: [PATCH] fpexif: Fix exif reader when pre-mature end-of-directory value is found in IFD. Issue #38904, patch by regs. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@8044 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- components/fpexif/fpeexifreadwrite.pas | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/components/fpexif/fpeexifreadwrite.pas b/components/fpexif/fpeexifreadwrite.pas index 28d5ee7f1..7d501af12 100644 --- a/components/fpexif/fpeexifreadwrite.pas +++ b/components/fpexif/fpeexifreadwrite.pas @@ -309,11 +309,13 @@ var i: Integer; ifdRec: TIFDRecord; byteCount: Integer; - data: TBytes; + data: TBytes = nil; n: Int64; tagPos: Int64; newPos: Int64; begin + ifdRec := Default(TIFDRecord); + // Read count of directory entries numRecords := FixEndian16(ReadWord(AStream)); if (AParent = TAGPARENT_THUMBNAIL) and (numRecords > 10) then begin @@ -326,7 +328,7 @@ begin AStream.Position := tagPos; // Read directory entry... n := SizeOf(ifdRec); - if AStream.Read(ifdRec{%H-}, n) < n then begin + if AStream.Read(ifdRec, n) < n then begin Error(Format(rsReadIncompleteIFDRecord, [tagPos])); exit; end; @@ -334,10 +336,17 @@ begin if (ifdRec.TagID = 0) and (ifdRec.DataType = 0) and (ifdRec.DataCount = 0) and (ifdRec.DataValue = 0) then begin // This is an empty IFD entry as found in images of the YUNEEC CGO3 camera - AStream.Position := AStream.Position + n; + // see: https://www.lazarusforum.de/viewtopic.php?f=18&t=13356 and + // https://bugs.freepascal.org/view.php?id=38904 + tagPos := tagPos + n; Continue; end; + if (ifdRec.TagID = 0) and (ifdRec.DataType = 0) then + // Unexpected end of directory (4 zero bytes), so breaking here. + // see: https://bugs.freepascal.org/view.php?id=38904 + Break; + ifdRec.TagID := FixEndian16(ifdRec.TagID); ifdRec.DataType := FixEndian16(ifdRec.DataType); if not (ifdRec.DataType in [1..ord(High(TTagType))]) then begin