Sign in Join

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

Mailing lists / fpc-devel / 2006-08

Found two Delphi incompatibilities

7 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

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

Sergei Gorelkin

Hello,

While porting some Delphi code, I found the following two incomatibility issues. Should I report them as bugs?

Sample 1: It compiles both in Delphi and FPC, but FPC executable fails at runtime. Delphi inserts temporary string variable and conversion (array of char -> string), but FPC treats the pointer literally. -----
const

  TestData: array[0..7] of Char = 'abc'#10'def'#0;
procedure Test1;
var
  sl: TStringList;
begin
  sl := TStringList.Create;
  sl.Text := string(@TestData[0]);  // <- fails here
  sl.Free;
end;
-----

Sample 2: This one compiles with Delphi (again, it inserts necessary conversion Wide -> Ansi), but does not compile with FPC, neither in objfpc nor in Delphi mode.

-----
function Test2: string;
var
  buf: array[0..255] of char;
  len: Integer;
begin
{ (skipped) I read USB string descriptor into buf. After that,
  buf[0] contains length of descriptor in bytes, buf[1] = 3,
  and rest is filled with Unicode chars. Now I want to convert it into
  ASCII...
}
  len := Integer(buf[0]) shr 1;
  SetString(result, PWideChar(@buf[2]), len-1);
end;
-----

Best regards,
Sergei

Reply by email

Michael Van Canneyt
to Sergei Gorelkin
Sergei Gorelkin wrote 8 lines
Hello, While porting some Delphi code, I found the following two incomatibility issues. Should I report them as bugs? Sample 1: It compiles both in Delphi and FPC, but FPC executable fails at runtime. Delphi inserts temporary string variable and conversion (array of char -> string), but FPC treats the pointer literally.

Did you check the '@ returns typed pointer' setting in FPC ?

Show quoted text 32 lines
----- const TestData: array[0..7] of Char = 'abc'#10'def'#0; procedure Test1; var sl: TStringList; begin sl := TStringList.Create; sl.Text := string(@TestData[0]); // <- fails here sl.Free; end; ----- Sample 2: This one compiles with Delphi (again, it inserts necessary conversion Wide -> Ansi), but does not compile with FPC, neither in objfpc nor in Delphi mode. ----- function Test2: string; var buf: array[0..255] of char; len: Integer; begin { (skipped) I read USB string descriptor into buf. After that, buf[0] contains length of descriptor in bytes, buf[1] = 3, and rest is filled with Unicode chars. Now I want to convert it into ASCII... } len := Integer(buf[0]) shr 1; SetString(result, PWideChar(@buf[2]), len-1); end;

What is the error ?

Michael.

Reply by email

Micha Nelissen
to Sergei Gorelkin: While porting some Delphi code, I found the following two incomatibility issues. Should I…
Sergei Gorelkin wrote 11 lines
const TestData: array[0..7] of Char = 'abc'#10'def'#0; procedure Test1; var sl: TStringList; begin sl := TStringList.Create; sl.Text := string(@TestData[0]); // <- fails here sl.Free; end;

A typecast is a typecast. Simply remove the typecast, and it should work ?

Sample 2: This one compiles with Delphi (again, it inserts necessary conversion Wide -> Ansi), but does not compile with FPC, neither in objfpc nor in Delphi mode.

The (should be) conversion code seems to be missing.

Micha

Reply by email

Sergei Gorelkin
to Michael Van Canneyt: Did you check the '@ returns typed pointer' setting in FPC ? What is the error ? Michael.

Thursday, August 31, 2006, 12:35:33 PM, Michael wrote:

Sergei Gorelkin wrote 8 lines
> Hello, > > While porting some Delphi code, I found the following two > incomatibility issues. Should I report them as bugs? > > Sample 1: It compiles both in Delphi and FPC, but FPC executable fails > at runtime. Delphi inserts temporary string variable and conversion > (array of char -> string), but FPC treats the pointer literally.

MVC> Did you check the '@ returns typed pointer' setting in FPC ?

Oops... That was a false alarm - FPC compiler behaves the same way as Delphi here. They both generate conversion code in {$T+} and do not generate it in {$T-}.
So my sample appears to be legal only in {$T+}, which isn't a default.

What makes a difference is actually TStrings.SetTextStr implementation. Delphi accesses the argument as null-terminated string, so it works perfectly well even without conversion. FPC implementation treats argument as AnsiString, accesses its Length field and eventually segfaults...

Show quoted text 32 lines
> ----- > const > TestData: array[0..7] of Char = 'abc'#10'def'#0; > > procedure Test1; > var > sl: TStringList; > begin > sl := TStringList.Create; > sl.Text := string(@TestData[0]); // <- fails here > sl.Free; > end; > ----- > > Sample 2: This one compiles with Delphi (again, it inserts necessary > conversion Wide -> Ansi), but does not compile with FPC, neither in > objfpc nor in Delphi mode. > > ----- > function Test2: string; > var > buf: array[0..255] of char; > len: Integer; > begin > { (skipped) I read USB string descriptor into buf. After that, > buf[0] contains length of descriptor in bytes, buf[1] = 3, > and rest is filled with Unicode chars. Now I want to convert it into > ASCII... > } > len := Integer(buf[0]) shr 1; > SetString(result, PWideChar(@buf[2]), len-1); > end;

MVC> What is the error ?

It is 'Incompatible type for arg no.2: Got "PWideChar", expected "PChar"'.

MVC> Michael.
MVC> _______________________________________________ MVC> fpc-devel maillist -
MVC> http://lists.freepascal.org/mailman/listinfo/fpc-devel

Best regards, Sergei

Reply by email

Michael Van Canneyt
to Sergei Gorelkin
Sergei Gorelkin wrote 25 lines
Thursday, August 31, 2006, 12:35:33 PM, Michael wrote: MVC> On Thu, 31 Aug 2006, Sergei Gorelkin wrote: >> Hello, >> >> While porting some Delphi code, I found the following two >> incomatibility issues. Should I report them as bugs? >> >> Sample 1: It compiles both in Delphi and FPC, but FPC executable fails >> at runtime. Delphi inserts temporary string variable and conversion >> (array of char -> string), but FPC treats the pointer literally. MVC> Did you check the '@ returns typed pointer' setting in FPC ? Oops... That was a false alarm - FPC compiler behaves the same way as Delphi here. They both generate conversion code in {$T+} and do not generate it in {$T-}. So my sample appears to be legal only in {$T+}, which isn't a default. What makes a difference is actually TStrings.SetTextStr implementation. Delphi accesses the argument as null-terminated string, so it works perfectly well even without conversion. FPC implementation treats argument as AnsiString, accesses its Length field and eventually segfaults...

We should check that.

Show quoted text 38 lines
>> ----- >> const >> TestData: array[0..7] of Char = 'abc'#10'def'#0; >> >> procedure Test1; >> var >> sl: TStringList; >> begin >> sl := TStringList.Create; >> sl.Text := string(@TestData[0]); // <- fails here >> sl.Free; >> end; >> ----- >> >> Sample 2: This one compiles with Delphi (again, it inserts necessary >> conversion Wide -> Ansi), but does not compile with FPC, neither in >> objfpc nor in Delphi mode. >> >> ----- >> function Test2: string; >> var >> buf: array[0..255] of char; >> len: Integer; >> begin >> { (skipped) I read USB string descriptor into buf. After that, >> buf[0] contains length of descriptor in bytes, buf[1] = 3, >> and rest is filled with Unicode chars. Now I want to convert it into >> ASCII... >> } >> len := Integer(buf[0]) shr 1; >> SetString(result, PWideChar(@buf[2]), len-1); >> end; MVC> What is the error ? It is 'Incompatible type for arg no.2: Got "PWideChar", expected "PChar"'.

Seems like a missing overloaded version of SetString()

Michael.

Reply by email

Daniël Mantione
to Sergei Gorelkin: MVC> Did you check the '@ returns typed pointer' setting in FPC ? Oops... That was a…

Op Thu, 31 Aug 2006, schreef Sergei Gorelkin:

What makes a difference is actually TStrings.SetTextStr implementation. Delphi accesses the argument as null-terminated string, so it works perfectly well even without conversion. FPC implementation treats argument as AnsiString, accesses its Length field and eventually segfaults...

I think FPC's implementation is correct, because, as I read the documentation, it isn't specified that the string cannot contain #0. So, you need to use the actual length of the string.

Daniël

Reply by email