From 274ebf2d716517dddb2297f252d2185acd88d65b Mon Sep 17 00:00:00 2001 From: skalogryz Date: Mon, 13 Jan 2020 15:09:10 +0000 Subject: [PATCH] richmemo: adding the check for a valid character offset, for most GetXXXX attributes methods. bug #32296 git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7282 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- components/richmemo/richmemo.pas | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/components/richmemo/richmemo.pas b/components/richmemo/richmemo.pas index 1f3df6423..3fff1ff05 100644 --- a/components/richmemo/richmemo.pas +++ b/components/richmemo/richmemo.pas @@ -206,6 +206,7 @@ type LinkStart, LinkEnd: Integer); function GetCanRedo: Boolean; virtual; + function isValidCharOfs(TextStart: integer): Boolean; public constructor Create(AOwner: TComponent); override; procedure CopyToClipboard; override; @@ -677,7 +678,8 @@ function TCustomRichMemo.GetTextAttributes(TextStart: Integer; var TextParams: T begin if not HandleAllocated then HandleNeeded; if HandleAllocated then - Result := TWSCustomRichMemoClass(WidgetSetClass).GetTextAttributes(Self, TextStart, TextParams) + Result := isValidCharOfs(TextStart) + and TWSCustomRichMemoClass(WidgetSetClass).GetTextAttributes(Self, TextStart, TextParams) else Result := false; end; @@ -686,7 +688,7 @@ function TCustomRichMemo.GetStyleRange(CharOfs: Integer; var RangeStart, RangeLen: Integer): Boolean; begin if HandleAllocated then begin - Result := TWSCustomRichMemoClass(WidgetSetClass).GetStyleRange(Self, CharOfs, RangeStart, RangeLen); + Result := isValidCharOfs(CharOfs) and TWSCustomRichMemoClass(WidgetSetClass).GetStyleRange(Self, CharOfs, RangeStart, RangeLen); if Result and (RangeLen = 0) then RangeLen := 1; end else begin RangeStart := -1; @@ -698,8 +700,9 @@ end; function TCustomRichMemo.GetParaAlignment(TextStart: Integer; var AAlign: TParaAlignment): Boolean; begin - Result := HandleAllocated and - TWSCustomRichMemoClass(WidgetSetClass).GetParaAlignment(Self, TextStart, AAlign); + Result := HandleAllocated + and isValidCharOfs(TextStart) + and TWSCustomRichMemoClass(WidgetSetClass).GetParaAlignment(Self, TextStart, AAlign); end; function TCustomRichMemo.GetParaAlignment(TextStart: Integer): TParaAlignment; @@ -718,7 +721,8 @@ function TCustomRichMemo.GetParaMetric(TextStart: Integer; var AMetric: TParaMetric): Boolean; begin if HandleAllocated then - Result := TWSCustomRichMemoClass(WidgetSetClass).GetParaMetric(Self, TextStart, AMetric) + Result := isValidCharOfs(TextStart) + and TWSCustomRichMemoClass(WidgetSetClass).GetParaMetric(Self, TextStart, AMetric) else Result := false; end; @@ -752,7 +756,8 @@ begin Result:=false; if not HandleAllocated then HandleNeeded; if HandleAllocated then - Result:=TWSCustomRichMemoClass(WidgetSetClass).GetParaRange(Self, CharOfs, ParaRange); + Result:=isValidCharOfs(CharOfs) + and TWSCustomRichMemoClass(WidgetSetClass).GetParaRange(Self, CharOfs, ParaRange); end; function TCustomRichMemo.GetParaRange(CharOfs: Integer; var TextStart, @@ -777,7 +782,8 @@ begin Result:=false; if not HandleAllocated then HandleNeeded; if HandleAllocated then - Result:=TWSCustomRichMemoClass(WidgetSetClass).GetParaTabs(Self, CharOfs, AStopList); + Result:= isValidCharOfs(CharOfs) + and TWSCustomRichMemoClass(WidgetSetClass).GetParaTabs(Self, CharOfs, AStopList); end; function TCustomRichMemo.GetContStyleLength(TextStart: Integer): Integer; @@ -1119,6 +1125,14 @@ begin Result:=false; end; +function TCustomRichMemo.isValidCharOfs(TextStart: integer): Boolean; +begin + // TextStart, where TextStart = GetTextLen, is a location at the end of the text + // it's technically a valid character offset (position) + // Because it's where the entry should occur + Result := (TextStart >= 0) and (TextStart <= GetTextLen); +end; + function TCustomRichMemo.PrintMeasure(const params: TPrintParams; var est: TPrintMeasure): Boolean; begin if not Assigned(Printer) then begin