From 16bfc42f8831a309f6078386ab1870d1278d925b Mon Sep 17 00:00:00 2001 From: Ansgar Becker Date: Tue, 20 Dec 2011 18:01:16 +0000 Subject: [PATCH] Do not access char at non existent position in a string. Introduces CharAtPos function. Fixes issue #2649. --- source/helpers.pas | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/source/helpers.pas b/source/helpers.pas index 9ff1df79..bbfbba7f 100644 --- a/source/helpers.pas +++ b/source/helpers.pas @@ -173,6 +173,7 @@ type procedure InvalidateVT(VT: TVirtualStringTree; RefreshTag: Integer; ImmediateRepaint: Boolean); procedure HandlePortableSettings(StartupMode: Boolean); function LoadConnectionParams(Session: String): TConnectionParameters; + function CharAtPos(Str: String; Pos: Integer): Char; function CompareAnyNode(Text1, Text2: String): Integer; function GetColumnDefaultType(var Text: String): TColumnDefaultType; function GetColumnDefaultClause(DefaultType: TColumnDefaultType; Text: String): String; @@ -2427,6 +2428,16 @@ begin end; +function CharAtPos(Str: String; Pos: Integer): Char; +begin + // Access char in string without causing access violation + if Length(Str) < Pos then + Result := #0 + else + Result := Str[Pos]; +end; + + function CompareAnyNode(Text1, Text2: String): Integer; var Number1, Number2 : Extended; @@ -2436,10 +2447,10 @@ const begin Result := 0; // Apply different comparisons for numbers and text - a1 := Text1[1]; - a2 := Text1[2]; - b1 := Text2[1]; - b2 := Text2[2]; + a1 := CharAtPos(Text1, 1); + a2 := CharAtPos(Text1, 2); + b1 := CharAtPos(Text2, 1); + b2 := CharAtPos(Text2, 2); if ((a1='-') and (CharInSet(a2, Numbers)) or CharInSet(a1, Numbers)) and ((b1='-') and (CharInSet(b2, Numbers)) or CharInSet(b1, Numbers)) then begin // Assuming numeric values