Recent

Author Topic: Bug in typecasting integer to array of characters  (Read 1619 times)

Avinash

  • Full Member
  • ***
  • Posts: 126
Bug in typecasting integer to array of characters
« on: November 17, 2023, 11:24:59 am »
Code: Pascal  [Select][+][-]
  1. type
  2.   LongRec = record
  3.     case byte of
  4.       0: (Lo, Hi: Word);
  5.       1: (Chars4: array [0..3] of Char);
  6.   end;
  7.  
  8. var I: LongInt;
  9.     S: String;
  10. begin
  11.  
  12.   S := 'test';
  13.  
  14.   I := $5A5A5A5A;  {'ZZZZ'}
  15.   WriteLn(LongRec(I).Chars4 + S);
  16.  
  17.   I := $5A5A5A;    {'ZZZ'#0}
  18.   WriteLn(LongRec(I).Chars4 + S);
  19.  
  20.   I := $5A5A;      {'ZZ'#0#0}
  21.   WriteLn(LongRec(I).Chars4 + S);
  22.  
  23.   I := $5A;        {'Z'#0#0#0}
  24.   WriteLn(LongRec(I).Chars4 + S);
  25.  
  26.   I := 0;          {#0#0#0#0}
  27.   WriteLn(LongRec(I).Chars4 + S);
  28. end.

Output by Borland TP (correct):
Quote
ZZZZtest
ZZZ test
ZZ  test
Z   test
    test

Output by FPC 3.2.2 Win32 (incorrect):
Quote
ZZZZtest
ZZZtest
ZZtest 
Ztest   
test   

Fibonacci

  • Hero Member
  • *****
  • Posts: 1000
  • Behold, I bring salvation - FPC Unleashed
Re: Bug in typecasting integer to array of characters
« Reply #1 on: November 17, 2023, 11:34:27 am »
I dont think its a bug, I would expect the output to look like it is looking. #0 is char array terminator, casted to string, then "s" appended, looks ok.
FPC Unleashed - inline vars, tuples, statement expressions, array equality, compound assignments, indexed/lazy labels, no-RTTI & more. ⭐ Star it on GitHub!

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12905
  • FPC developer.
Re: Bug in typecasting integer to array of characters
« Reply #2 on: November 17, 2023, 12:05:37 pm »
It might not even be Free Pascal, but merely the change of OS output functionality. TP might even differ between using Crt and not.

Avinash

  • Full Member
  • ***
  • Posts: 126
Re: Bug in typecasting integer to array of characters
« Reply #3 on: November 17, 2023, 12:18:00 pm »
The output has nothing to do with it, it was originally discovered in a binary context.

Code: Pascal  [Select][+][-]
  1. type
  2.   LongRec = record
  3.     case byte of
  4.       0: (Lo, Hi: Word);
  5.       1: (Chars4: array [0..3] of Char);
  6.   end;
  7.  
  8. var I: LongInt;
  9.     S: String;
  10. begin
  11.   I := $5A5A5A5A; {'ZZZZ'}
  12.   S := LongRec(I).Chars4;
  13.   WriteLn(Length(S):2,' ',S);
  14.  
  15.   I := $5A00005A; {'Z'#0#0'Z'}
  16.   S := LongRec(I).Chars4;
  17.   WriteLn(Length(S):2,' ',S);
  18. end.

FPC 3.2.2 Win32:
Quote

 4 ZZZZ
 1 Z

Fibonacci

  • Hero Member
  • *****
  • Posts: 1000
  • Behold, I bring salvation - FPC Unleashed
Re: Bug in typecasting integer to array of characters
« Reply #4 on: November 17, 2023, 12:25:24 pm »
So whats wrong? $5A00005A as PChar casted to String will have length of 1
FPC Unleashed - inline vars, tuples, statement expressions, array equality, compound assignments, indexed/lazy labels, no-RTTI & more. ⭐ Star it on GitHub!

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12905
  • FPC developer.
Re: Bug in typecasting integer to array of characters
« Reply #5 on: November 17, 2023, 12:52:30 pm »
I think his beef is that pchar automated conversions are afaik a Delphi thing.


Avinash

  • Full Member
  • ***
  • Posts: 126
Re: Bug in typecasting integer to array of characters
« Reply #6 on: November 17, 2023, 01:18:02 pm »
Discovered thing:

Code: Pascal  [Select][+][-]
  1. var Q0: array [0..3] of Char;
  2.     Q1: array [1..4] of Char;
  3. begin
  4.   Q0 := 'Z'#0#0'Z';
  5.   Q1 := 'Z'#0#0'Z';
  6.   WriteLn(Q0);
  7.   WriteLn(Q1);
  8. end.

Output:
Quote
Z   
Z  Z

Fibonacci

  • Hero Member
  • *****
  • Posts: 1000
  • Behold, I bring salvation - FPC Unleashed
Re: Bug in typecasting integer to array of characters
« Reply #7 on: November 17, 2023, 01:40:19 pm »
Is it a bug if its done on purpose?

zerobased = as PAnsiChar (to first nil)
otherwise = print full array

Code: Pascal  [Select][+][-]
  1. Procedure fpc_Write_Text_Pchar_as_Array(Len : Longint;var f : Text;const s : array of ansichar; zerobased: boolean = true); iocheck; compilerproc;
  2. var
  3.   ArrayLen : longint;
  4.   p : pansichar;
  5. Begin
  6.   If (InOutRes<>0) then
  7.    exit;
  8.   case TextRec(f).mode of
  9.     fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  10.       begin
  11.         p:=pansichar(@s);
  12.         if zerobased then
  13.           begin
  14.             { can't use StrLen, since that one could try to read past the end }
  15.             { of the heap (JM)                                                }
  16.             ArrayLen:=IndexByte(p^,high(s)+1,0);
  17.             { IndexByte returns -1 if not found (JM) }
  18.             if ArrayLen = -1 then
  19.               ArrayLen := high(s)+1;
  20.           end
  21.         else
  22.           ArrayLen := high(s)+1;
  23.         If Len>ArrayLen Then
  24.           fpc_WriteBlanks(f,Len-ArrayLen);
  25.         fpc_WriteBuffer(f,p^,ArrayLen);
  26.       end;
  27.     fmInput: InOutRes:=105
  28.     else InOutRes:=103;
  29.   end;
  30. End;

PS. Thats something different than your first post
« Last Edit: November 17, 2023, 01:45:16 pm by Fibonacci »
FPC Unleashed - inline vars, tuples, statement expressions, array equality, compound assignments, indexed/lazy labels, no-RTTI & more. ⭐ Star it on GitHub!

Warfley

  • Hero Member
  • *****
  • Posts: 2067
Re: Bug in typecasting integer to array of characters
« Reply #8 on: November 17, 2023, 02:34:16 pm »
This is at most a bug in the implementation of the write intrinsic, and even then I would not be so sure about that.

Static char Arrays are often used to interface with C apis such as Berkeley sockets. C strings are #0 terminated, therefore treating static char Arrays as C Strings actually makes quite some sense.

nanobit

  • Full Member
  • ***
  • Posts: 192
Re: Bug in typecasting integer to array of characters
« Reply #9 on: November 17, 2023, 03:49:46 pm »
zerobased = as PAnsiChar (to first nil)
otherwise = print full array

This is by design. Only zero-based array is compatible with PChar.
Then string( array) is like string( pchar). This is also Delphi compatible.

 

TinyPortal © 2005-2018