Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2008-05

WARNING: Widestring Delete function causes crashes (FPC 2.2.0)

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Tobias Giesen

Hello,

the Widestring Delete function in wstrings.inc accesses memory beyond the end of the string. This causes crashes after a while. The Delete function has been fixed in FPC 2.3 but the fix must be copied to the earlier branches too. I have submitted this as a bug in Mantis: http://bugs.freepascal.org/view.php?id=11365

Until it is fixed, developers can just use the fixed code below instead of the one in the System unit. You just need to find out if your platform uses FPC_WINLIKEWIDESTRING or not. On my platform, MacOS X, this is not defined.

Type
  PWideRec = ^TWideRec;
  TWideRec = Packed Record
    {$ifdef FPC_WINLIKEWIDESTRING}
    Len   : DWord;
    {$else FPC_WINLIKEWIDESTRING}
    Ref : SizeInt;
    Len : SizeInt;
    {$endif FPC_WINLIKEWIDESTRING}
    First : WideChar;
  end;
Const
  WideRecLen = SizeOf(TWideRec);
  WideFirstOff = SizeOf(TWideRec)-sizeof(WideChar);
Procedure Delete (Var S : WideString; Index,Size: SizeInt); overload;
Var
  LS : SizeInt;
begin
  If Length(S)=0 then
     exit;
  if index<=0 then
     exit;
  LS:=PWideRec(Pointer(S)-WideFirstOff)^.Len div sizeof(WideChar);
  if (Index<=LS) and (Size>0) then begin
     UniqueString (S);
     if Size+Index>LS then
        Size:=LS-Index+1;
     if Index+Size<=LS then begin
        Dec(Index);
        System.Move(PWideChar(S)[Index+Size],PWideChar(S)[Index],
              (LS-Index-Size+1)*sizeof(WideChar));
        end;
     Setlength(s,LS-Size);
     end;
  end;

Kind Regards,
Tobias Giesen
http://www.superflexible.com

Jonas Maebe
to Tobias Giesen
Tobias Giesen wrote 5 lines
the Widestring Delete function in wstrings.inc accesses memory beyond the end of the string. This causes crashes after a while. The Delete function has been fixed in FPC 2.3 but the fix must be copied to the earlier branches too. I have submitted this as a bug in Mantis: http://bugs.freepascal.org/view.php?id=11365

As I noted in your bug report, this has been fixed in 2.2.1 for several months already.

Jonas

Tobias Giesen
to Jonas Maebe
As I noted in your bug report, this has been fixed in 2.2.1 for several months already.

But the fix must be copied to 2.2.0 too, because the Lazarus team is still spreading this crash on their daily snapshots. I don't know who is responsible for that. Can you tell me?

Cheers,
Tobias

Jonas Maebe
to Tobias Giesen
Tobias Giesen wrote 5 lines
> As I noted in your bug report, this has been fixed in 2.2.1 for > several months already. But the fix must be copied to 2.2.0 too, because the Lazarus team is still spreading this crash on their daily snapshots.

Nothing will be merged into 2.2.0. It is a fixed/official release and there is only one single source collection called FPC 2.2.0. On the other hand, 2.2.1 and 2.3.1 are snapshot versions and every single commit in principle creates a new source collection representing these versions, i.e. these are in constant flux.

Once the next official version is out (2.2.2), that one again will be fixed and its snapshot version will become 2.2.3 (which will be under continuous development).

Jonas