From acda1ecd09e1aff7c2b9bbdca6e820021c4c9e4c Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Mon, 6 May 2019 15:50:39 +0000 Subject: [PATCH] fpspreadsheet: Fix memory leak due to rich-text streams in shared string table when reading biff8. Issue #35528, modified patch by Jurijs Romanovs. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@6891 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../fpspreadsheet/source/common/xlsbiff8.pas | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/components/fpspreadsheet/source/common/xlsbiff8.pas b/components/fpspreadsheet/source/common/xlsbiff8.pas index dfdcd6ec7..1d68a6253 100644 --- a/components/fpspreadsheet/source/common/xlsbiff8.pas +++ b/components/fpspreadsheet/source/common/xlsbiff8.pas @@ -124,6 +124,7 @@ type FCommentLen: Integer; FBiff8ExternBooks: TsBiff8ExternBookList; FBiff8ExternSheetArray: array of TsBiff8ExternSheet; + procedure FreeSharedStringTable; function ReadString(const AStream: TStream; const ALength: Word; out ARichTextParams: TsRichTextParams): String; function ReadUnformattedWideString(const AStream: TStream; @@ -786,8 +787,8 @@ begin SetLength(FBiff8ExternSheetArray, 0); FBiff8ExternBooks.Free; - { Destroy shared string table } - FSharedStringTable.Free; + { Destroy shared string table, as well as associated rich-text streams} + FreeSharedStringTable; { Destroy comment list } FCommentList.Free; @@ -813,6 +814,19 @@ begin end; end; +{@@ ---------------------------------------------------------------------------- + Frees the shared string table. Also: destroys the rich-text memory streams + which can be assigned to string table items. +-------------------------------------------------------------------------------} +procedure TsSpreadBIFF8Reader.FreeSharedStringTable; +var + j: Integer; +begin + for j := 0 to FSharedStringTable.Count-1 do + TObject(FSharedStringTable.Objects[j]).Free; + FreeAndNil(FSharedStringTable); +end; + {@@ ---------------------------------------------------------------------------- Populates the reader's default palette using the BIFF8 default colors. -------------------------------------------------------------------------------} @@ -1122,8 +1136,7 @@ begin then FCommentList := TObjectList.Create else FCommentList.Clear; - if Assigned(FSharedStringTable) then - FreeAndNil(FSharedStringTable); + FreeSharedStringTable; while (not SectionEOF) do begin { Read the record header }