Recent

Author Topic: How to prevent automate assign of PChar to String  (Read 2108 times)

Okoba

  • Hero Member
  • *****
  • Posts: 555
How to prevent automate assign of PChar to String
« on: February 23, 2024, 09:19:30 pm »
For now, FPC automatically set a PChar to a String value and I want to disallow it for safety. Is there a way?
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. var
  4.   A: PChar;
  5.   B: String;
  6. begin
  7.   A := GetMem(3);
  8.   A[0] := 'x';
  9.   A[1] := 'y';
  10.   A[2] := #0;
  11.   B := A; //Assign automatically
  12.   WriteLn(B);
  13.   ReadLn;
  14. end.                                          

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: How to prevent automate assign of PChar to String
« Reply #1 on: February 23, 2024, 09:28:17 pm »
For now, FPC automatically set a PChar to a String value and I want to disallow it for safety. Is there a way?
It is not FPC, it is you that command "B := A;"
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$APPTYPE CONSOLE}
  3. var
  4.   ps: PChar;
  5.   s: string;
  6. begin
  7.   s := 'FPC does not automagical assign anything to me.';
  8.   ps := 'yx';
  9.   WriteLn(ps);
  10.   WriteLn(s);
  11.   ReadLn;
  12. end.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Okoba

  • Hero Member
  • *****
  • Posts: 555
Re: How to prevent automate assign of PChar to String
« Reply #2 on: February 23, 2024, 09:35:02 pm »
I was not clear. I dont want FPC to let me write that code. I want to get an error. Like when you want to assign an integer to a string and compiler raises an error.

Thaddy

  • Hero Member
  • *****
  • Posts: 16168
  • Censorship about opinions does not belong here.
Re: How to prevent automate assign of PChar to String
« Reply #3 on: February 23, 2024, 09:42:08 pm »
Ignore the error, because it is parsed ...
Code: Pascal  [Select][+][-]
  1. program Project1;{$H+}
  2. {$macro on}{$Define string:=}
  3. var
  4.   A: PChar;
  5.   B: String;
  6. begin
  7.   A := GetMem(3);
  8.   A[0] := 'x';
  9.   A[1] := 'y';
  10.   A[2] := #0;
  11.   B := A; //Assign automatically
  12.   WriteLn(B);
  13.   ReadLn;
  14. end.
         

And it is the world upside down: Pascal strings are safe, Pchars are not....       
And please call freemem.....                         
« Last Edit: February 23, 2024, 09:48:51 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: How to prevent automate assign of PChar to String
« Reply #4 on: February 23, 2024, 09:47:52 pm »
I was not clear. I dont want FPC to let me write that code. I want to get an error. Like when you want to assign an integer to a string and compiler raises an error.
Beside killing a type like Thaddy I guess showed, no. That is part of FPC design.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

ASerge

  • Hero Member
  • *****
  • Posts: 2337
Re: How to prevent automate assign of PChar to String
« Reply #5 on: February 23, 2024, 10:16:04 pm »
For now, FPC automatically set a PChar to a String value and I want to disallow it for safety. Is there a way?
{$MODE TP}

PascalDragon

  • Hero Member
  • *****
  • Posts: 5755
  • Compiler Developer
Re: How to prevent automate assign of PChar to String
« Reply #6 on: February 23, 2024, 11:03:41 pm »
For now, FPC automatically set a PChar to a String value and I want to disallow it for safety. Is there a way?

No, there is not. Also there isn't anything dangerous about assinging a PChar to a String. What would be “dangerous” would be the other way around, because when you change the String variable you'd also change the PChar value which might not be desired.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11938
  • FPC developer.
Re: How to prevent automate assign of PChar to String
« Reply #7 on: February 23, 2024, 11:10:42 pm »
(not every pointer to char is necessarily 0-terminated)

PascalDragon

  • Hero Member
  • *****
  • Posts: 5755
  • Compiler Developer
Re: How to prevent automate assign of PChar to String
« Reply #8 on: February 23, 2024, 11:23:52 pm »
(not every pointer to char is necessarily 0-terminated)

Okay, I'll grant you that 😅

Okoba

  • Hero Member
  • *****
  • Posts: 555
Re: How to prevent automate assign of PChar to String
« Reply #9 on: February 23, 2024, 11:46:27 pm »
Exactly what marcov said. And Some times, I have PChar that is part of another string, so not dangerous but creates problems, so I prefer disallowing it together

@ASerge thank you. I wished I could use TP mode for this.

@PascalDragon is not a mode switch for it to use the error just like TP mode does it? If not, is it okay to make an issue about it?

ASerge

  • Hero Member
  • *****
  • Posts: 2337
Re: How to prevent automate assign of PChar to String
« Reply #10 on: February 24, 2024, 09:39:43 am »
It was expected that {$MODESWITCH PCHARTOSTRING-} would work, but alas.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5755
  • Compiler Developer
Re: How to prevent automate assign of PChar to String
« Reply #11 on: February 25, 2024, 05:07:27 pm »
It was expected that {$MODESWITCH PCHARTOSTRING-} would work, but alas.

Seems like that modeswitch isn't used anywhere. Don't know right now if it ever hadn't been used or if it's usage got lost some time in the past... maybe indeed worth a bug report? 🤔

Okoba

  • Hero Member
  • *****
  • Posts: 555
Re: How to prevent automate assign of PChar to String
« Reply #12 on: February 27, 2024, 09:43:02 am »
@PascalDragon I opened an issue with a patch suggestion: https://gitlab.com/freepascal.org/fpc/source/-/issues/40669

cdbc

  • Hero Member
  • *****
  • Posts: 1655
    • http://www.cdbc.dk
Re: How to prevent automate assign of PChar to String
« Reply #13 on: February 27, 2024, 10:39:09 am »
Hi
@Okoba: Would your patch, if applied, mean that we'd have to fiddle with "{$MODESWITCH PCHARTOSTRING-/+}" in every /dang/ unit?!? I'm not quite sure I like that...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Okoba

  • Hero Member
  • *****
  • Posts: 555
Re: How to prevent automate assign of PChar to String
« Reply #14 on: February 27, 2024, 11:00:53 am »
Everything should work as before as PCHARTOSTRING+ seems to be as the default for modes other that TP. But if you want to prevent such automatics assignment in modes other than TP you can add PCHARTOSTRING-.

 

TinyPortal © 2005-2018