Recent

Author Topic: Unexpected FPC v3.0.4 Behavior with WRITEABLECONST OFF  (Read 2391 times)

440bx

  • Hero Member
  • *****
  • Posts: 3945
Unexpected FPC v3.0.4 Behavior with WRITEABLECONST OFF
« 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.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Unexpected FPC v3.0.4 Behavior with WRITEABLECONST OFF
« Reply #1 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.
« Last Edit: May 09, 2021, 09:52:18 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

440bx

  • Hero Member
  • *****
  • Posts: 3945
Re: Unexpected FPC v3.0.4 Behavior with WRITEABLECONST OFF
« Reply #2 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.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Unexpected FPC v3.0.4 Behavior with WRITEABLECONST OFF
« Reply #3 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".

440bx

  • Hero Member
  • *****
  • Posts: 3945
Re: Unexpected FPC v3.0.4 Behavior with WRITEABLECONST OFF
« Reply #4 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.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

440bx

  • Hero Member
  • *****
  • Posts: 3945
Re: Unexpected FPC v3.0.4 Behavior with WRITEABLECONST OFF
« Reply #5 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 ?

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Unexpected FPC v3.0.4 Behavior with WRITEABLECONST OFF
« Reply #6 on: May 11, 2021, 02:06:48 pm »
Yes, if only since it is not Delphi compatible.

Bart

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Unexpected FPC v3.0.4 Behavior with WRITEABLECONST OFF
« Reply #7 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. :)

440bx

  • Hero Member
  • *****
  • Posts: 3945
Re: Unexpected FPC v3.0.4 Behavior with WRITEABLECONST OFF
« Reply #8 on: May 12, 2021, 10:27:08 am »
reported.  Ticket : 0038877
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018