Hi.

Sysstr comparing routines does not work correct if two strings were
empty. Example:

program comp;
uses
   SysUtils;
var
  a, b: array[0..1] of char;
begin
  a[0] := #0;
  a[1] := #1;
  b[0] := #0;
  b[1] := #0;
  writeln(AnsiStrComp(a, b)=0); // '',''=0
end

This program returns false and theoreticaly repeat..until may SIGSEGV
because overflows empty PChar boundary after first cycle.

Patch is in the attachment.

Petr

-- 
Ing. Petr Kristan
.
EPOS PRO s.r.o., Bozeny Nemcove 2625, 530 02 Pardubice
tel: +420 466335223    Czech Republic (Eastern Europe) 
fax: +420 466510709
Index: objpas/sysutils/sysstr.inc
===================================================================
--- objpas/sysutils/sysstr.inc	(revision 10368)
+++ objpas/sysutils/sysstr.inc	(working copy)
@@ -307,11 +307,12 @@
       Result:=1;
       exit;
     end;
-  Repeat
+  While (Result=0) and (S1^<>#0) and (S2^<>#0) do 
+    begin
     Result:=Ord(S1^)-Ord(S2^); //!! Must be replaced by ansi characters !!
     Inc(S1);
     Inc(S2);
-  Until (Result<>0) or (S1^=#0) or (S2^=#0);
+    end;
   if (Result=0) and (S1^<>S2^) then // loop ended because exactly one has #0
     if S1^=#0 then // shorter string is smaller
       result:=-1
@@ -335,11 +336,12 @@
     Result:=1;
     exit;
     end;
-  Repeat
+  While (Result=0) and (S1^<>#0) and (S2^<>#0) do 
+    begin
     Result:=Ord(LowerCaseTable[Ord(S1[0])])-Ord(LowerCaseTable[Ord(S2[0])]); //!! Must be replaced by ansi characters !!
     Inc(S1);
     Inc(S2);
-  Until (Result<>0) or ((S1[0]=#0) or (S2[0]=#0))
+    end;
 end;
 
 
@@ -362,12 +364,13 @@
     exit;
     end;
   I:=0;
-  Repeat
+  While (Result=0) and (S1^<>#0) and (S2^<>#0) and (I<MaxLen) do 
+    begin
     Result:=Ord(S1[0])-Ord(S2[0]); //!! Must be replaced by ansi characters !!
     Inc(S1);
     Inc(S2);
     Inc(I);
-  Until (Result<>0) or ((S1[0]=#0) or (S2[0]=#0)) or (I=MaxLen)
+    end;
 end;
 
 
@@ -390,12 +393,13 @@
     exit;
     end;
   I:=0;
-  Repeat
+  While (Result=0) and (S1^<>#0) and (S2^<>#0) and (I<MaxLen) do 
+    begin
     Result:=Ord(LowerCaseTable[Ord(S1[0])])-Ord(LowerCaseTable[Ord(S2[0])]); //!! Must be replaced by ansi characters !!
     Inc(S1);
     Inc(S2);
     Inc(I);
-  Until (Result<>0) or ((S1[0]=#0) or (S2[0]=#0)) or (I=MaxLen)
+    end;
 end;
 
 
_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply via email to