Skip to content

Commit 53df50a

Browse files
committed
fix: wrong IsHex result on non-MySQL connections, prevent escaping quotes in SQLite hex values x'aa...'
Refs #1980 Co-authored-by: ansgarbecker <ansgarbecker@users.noreply.github.com>
1 parent 2c48500 commit 53df50a

1 file changed

Lines changed: 23 additions & 15 deletions

File tree

‎source/dbconnection.pas‎

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5274,30 +5274,33 @@ function TDBConnection.EscapeString(Text: String; ProcessJokerChars: Boolean=fal
52745274

52755275
function TDBConnection.EscapeString(Text: String; Datatype: TDBDatatype): String;
52765276
var
5277-
DoQuote: Boolean;
5277+
DoEscape: Boolean;
52785278
ValuePrefix: String;
52795279
const
52805280
CategoriesNeedQuote = [dtcText, dtcBinary, dtcTemporal, dtcSpatial, dtcOther];
52815281
begin
5282-
// Quote text based on the passed datatype
5283-
DoQuote := Datatype.Category in CategoriesNeedQuote;
5282+
// Escape and quote text based on the passed datatype
5283+
DoEscape := Datatype.Category in CategoriesNeedQuote;
52845284
ValuePrefix := '';
52855285
case Datatype.Category of
52865286
// Some special cases
52875287
dtcBinary, dtcSpatial: begin
52885288
if IsHex(Text) then
5289-
DoQuote := False;
5289+
DoEscape := False;
52905290
end;
52915291
dtcInteger, dtcReal: begin
52925292
if (not IsNumeric(Text)) and (not IsHex(Text)) then
5293-
DoQuote := True;
5293+
DoEscape := True;
52945294
if (Datatype.Index = dbdtBit) and FParameters.IsAnyMySQL then begin
5295-
DoQuote := True;
5295+
DoEscape := True;
52965296
ValuePrefix := 'b';
52975297
end;
52985298
end;
52995299
end;
5300-
Result := ValuePrefix + EscapeString(Text, False, DoQuote);
5300+
if DoEscape then
5301+
Result := ValuePrefix + EscapeString(Text, False)
5302+
else
5303+
Result := ValuePrefix + Text;
53015304
end;
53025305

53035306

@@ -6773,23 +6776,28 @@ function TDBConnection.IsNumeric(Text: String): Boolean;
67736776

67746777
function TDBConnection.IsHex(Text: String): Boolean;
67756778
var
6776-
i, Len: Integer;
6779+
i, BodyLen: Integer;
6780+
HexBody: String;
67776781
const
67786782
HexChars: TSysCharSet = ['0'..'9','a'..'f', 'A'..'F'];
67796783
begin
67806784
// Check first kilobyte of passed text whether it's a hex encoded string. Hopefully faster than a regex.
67816785
Result := False;
6782-
Len := Length(Text);
6783-
if Len >= 3 then begin
6784-
Result := (FHexPrefix.IsEmpty or Text.StartsWith(FHexPrefix)) and (FHexPostfix.IsEmpty or Text.EndsWith(FHexPostfix));
6786+
BodyLen := Length(Text) - Length(FHexPrefix) - Length(FHexPostfix);
6787+
if (BodyLen >= 0) and (not Odd(BodyLen)) then begin
6788+
Result := (FHexPrefix.IsEmpty or Text.StartsWith(FHexPrefix)) and
6789+
(FHexPostfix.IsEmpty or Text.EndsWith(FHexPostfix));
67856790
if Result then begin
6786-
for i:=3 to SIZE_KB do begin
6787-
if not CharInSet(Text[i], HexChars) then begin
6791+
HexBody := Text;
6792+
if not FHexPrefix.IsEmpty then
6793+
Delete(HexBody, 1, Length(FHexPrefix));
6794+
if not FHexPostfix.IsEmpty then
6795+
Delete(HexBody, Length(HexBody) - Length(FHexPostfix) + 1, Length(FHexPostfix));
6796+
for i:=1 to Length(HexBody) do begin
6797+
if not CharInSet(HexBody[i], HexChars) then begin
67886798
Result := False;
67896799
Break;
67906800
end;
6791-
if i >= Len then
6792-
Break;
67936801
end;
67946802
end;
67956803
end;

0 commit comments

Comments
 (0)