Sign in Join

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

Mailing lists / fpc-pascal / 2002-03

"Bug" in ansistrings

1 message · 1 person ·

Reply by email Subscribe to this list Your subscription

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

RRC2Soft

(*
Hello all!
There's a "bug" in ansistrings. When you concat 2 ansistrings (one NIL and one
with something), both of them have their pointers to the same address. And when
the assignment made below is made, both are modified. I say it's a "bug" 'cause ansistrings are used in the program in a very strange
way (Direct Memory access). But Delphi allows this, and some programs use this
feature (Innerfuse Pascal Script v3.0 as an example, i'm trying to port it to
FPK).
See ya!!!

Rodrigo (Rui) R. [1/2 RRC²Soft]

  PD: Innerfuse Pascal Script 3: http://carlo-kok.com
*)
// Data of snapshot:
// Free Pascal Compiler version 1.0.5 [2002/02/28] for i386
//=========================================================
Program AnsiBug;
{$MODE DELPHI}
{$H+}
{$PACKRECORDS 1}
var temp,stack:ansistring;
    i:char;
function GetPtr(const i:char): ansistring;
Var x:Ansistring;
  begin
    x:= #0#0#0#0;                {Yes, this's kinda strange...}
    LongInt((@x[1])^) := Ord(i); {...but this feature can be used...}
    Result:=x;                   {... for simulating dinamic arrays}
  end;
begin
   Stack:='';
   For i:= 'D' downto 'A' do
   begin
   	  WriteLn(' Old Stack  :',Stack);
          {>_<}Temp := GetPtr(i); { Temp='C', also Stack='C'??? }
   	  WriteLn(' Old? Stack :',Stack);
          if Temp = '' then exit;
          {>_<}Stack := Stack + Temp;{<-Same address (Stack&Temp) if
Stack=''}
          WriteLn(' ===================');
   end;
   WriteLn(Stack);
end.