Lazarus

Free Pascal => General => Topic started by: 440bx on May 09, 2021, 09:09:02 pm

Title: Unexpected FPC v3.0.4 Behavior with WRITEABLECONST OFF
Post by: 440bx on May 09, 2021, 09:09:02 pm
Hello,

FPC (v3.0.4) does not catch all attempts to write to non-writeable constants even when they are evident.  Consider the test program below:

Code: Pascal  [Select][+][-]
  1. {$APPTYPE        CONSOLE}
  2.  
  3. {$TYPEDADDRESS   ON}
  4.  
  5. {$LONGSTRINGS    OFF}
  6. {$WRITEABLECONST OFF}
  7.  
  8. { --------------------------------------------------------------------------- }
  9.  
  10. program _writeableconst;
  11.  
  12. const
  13.   ArrayConst : array[0..1] of
  14.                record
  15.                  t : pchar;
  16.                  l : DWORD
  17.                end =
  18.   (
  19.    (t: 'some text'; l: 0),
  20.    (t: 'more text'; l: 0)
  21.   );
  22.  
  23.   v          : DWORD = 0;
  24.  
  25. var
  26.   i          : integer;
  27.  
  28. begin
  29.   for i := low(ArrayConst) to high(ArrayConst) do
  30.   begin
  31.     with ArrayConst[i] do
  32.     begin
  33.       { assign to a non writeable constant, yet NO compiler error emitted     }
  34.       { BUT, you get an access violation at runtime (as there should be)      }
  35.  
  36.       l := i + 10;            { set to any value                              }
  37.     end;
  38.   end;
  39.  
  40.   { this statement causes a compile time error - as it should                 }
  41.  
  42.   v := 1;    { compile error - as expected                                    }
  43. end.              

The compiler rightfully complains about the statement in line 42 about the attempt to write to a non-writeable constant but, says _nothing_ about line 36 which, isn't really different than line 42.

If line 42 is commented out, the program compiles and when run causes an access violation because the constant is in a read-only segment.

Given that I've only tested this using FPC v3.0.4, I have not filed a bug since it won't be corrected in that old version but, it's probably worth checking how the newer FPC versions behave in that case.

HTH.
Title: Re: Unexpected FPC v3.0.4 Behavior with WRITEABLECONST OFF
Post by: lucamar on May 09, 2021, 09:48:57 pm
Version 3.2.0 does the same: it stops compilation on line 42 but ignores the equally "wrong" line 36.

Only difference (linux-gtk2 x86_64) is it doesn't cause an access violation; it behaves as if the constant record was indeed writeable.
Title: Re: Unexpected FPC v3.0.4 Behavior with WRITEABLECONST OFF
Post by: 440bx on May 09, 2021, 11:10:14 pm
Only difference (linux-gtk2 x86_64) is it doesn't cause an access violation; it behaves as if the constant record was indeed writeable.
Same with Delphi 2 on Windows.  Delphi 2 puts the typed constants in a writeable data segment regardless of the setting of WRITEABLECONST.  (hopefully they changed that in later versions.)

Thank you for checking out how the more recent version behaves.
Title: Re: Unexpected FPC v3.0.4 Behavior with WRITEABLECONST OFF
Post by: ASerge on May 09, 2021, 11:30:43 pm
Version 3.2.0 does the same: it stops compilation on line 42 but ignores the equally "wrong" line 36.
FPC 3.3.1 (at least revision 49230) also ignores the equally "wrong" line 36.

Same with Delphi 2 on Windows.
Delphi 10.3 reports "[dcc64 Error] Project1.dpr(36): E2064 Left side cannot be assigned to".
Title: Re: Unexpected FPC v3.0.4 Behavior with WRITEABLECONST OFF
Post by: 440bx on May 09, 2021, 11:37:59 pm
Delphi 10.3 reports "[dcc64 Error] Project1.dpr(36): E2064 Left side cannot be assigned to".
Thank you for reporting that Serge.  Nice that they fixed that.
Title: Re: Unexpected FPC v3.0.4 Behavior with WRITEABLECONST OFF
Post by: 440bx on May 11, 2021, 08:48:01 am
Now, the question is, should the fact that FPC does not reject line 36 (assignment to a non-writeable constant) be reported as a bug ?

Title: Re: Unexpected FPC v3.0.4 Behavior with WRITEABLECONST OFF
Post by: Bart on May 11, 2021, 02:06:48 pm
Yes, if only since it is not Delphi compatible.

Bart
Title: Re: Unexpected FPC v3.0.4 Behavior with WRITEABLECONST OFF
Post by: PascalDragon on May 12, 2021, 09:10:09 am
Now, the question is, should the fact that FPC does not reject line 36 (assignment to a non-writeable constant) be reported as a bug ?

It could even be that it was already reported, cause the issue does indeed sound familiar... But you can report it nevertheless and if it should turn out to be a duplicate of an existing (probably still open) issue, then it will be resolved as such. :)
Title: Re: Unexpected FPC v3.0.4 Behavior with WRITEABLECONST OFF
Post by: 440bx on May 12, 2021, 10:27:08 am
reported.  Ticket : 0038877
TinyPortal © 2005-2018