Sign in Join

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

Mailing lists / lazarus / 2008-05

WARNING: Widestring Delete function causes crashes

3 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

Reply by email

Tobias Giesen
to Tobias Giesen

Hi,

OK I learned that it won't be fixed in 2.2.0, which surely justifies my warning. I wish I had been warned, since this has been fixed in 2.2.1 a long time ago. But only 2.2.0 is available on the daily snapshots page, so this is really a trap.

Could FPC 2.2.1 for Mac OS be made available on the daily snapshots?

Cheers,
Tobias

Reply by email

Vincent Snijders
to Tobias Giesen
Tobias Giesen wrote 9 lines
Hi, OK I learned that it won't be fixed in 2.2.0, which surely justifies my warning. I wish I had been warned, since this has been fixed in 2.2.1 a long time ago. But only 2.2.0 is available on the daily snapshots page, so this is really a trap. Could FPC 2.2.1 for Mac OS be made available on the daily snapshots?

Yes, since our next release will be based on 2.2.1 (actually 2.2.2) and 2.2.1 is getting stable now, so we don't run into much additional risk by using an development fpc version.

Vincent

Reply by email