Unexpected FPC v3.0.4 Behavior with WRITEABLECONST OFF lazarus / 62-General ---------------------------------------------------------------------- 440bx · 2021-05-09 19:09 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$APPTYPE      CONSOLE} {$TYPEDADDRESS   ON} {$LONGSTRINGS    OFF}{$WRITEABLECONST OFF} { ----------------------------------------- ---------------------------------- } program _writeableconst; const  ArrayConst : array[0..1] of               record                 t : pchar;                 l : DWORD               end =  (   (t: 'some text'; l: 0),   (t: 'more text'; l: 0)  );   v          : DWORD = 0; var  i          : integer; begin  for i := low(ArrayConst) to high(ArrayConst) do  begin    with ArrayConst[i] do    begin      { assign to a non writeable constant, yet NO compiler error emitted     }      { BUT, you get an access violation at runtime (as there should be)      }       l := i + 10;            { set to any value                            }    end;  end;   { this statement causes a compile time error - as it should                 }   v := 1;    { compile error - as expected                                    }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. ---------------------------------------------------------------------- lucamar · 2021-05-09 19:48 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. ---------------------------------------------------------------------- 440bx · 2021-05-09 21:10 Quote from: lucamar on May 09, 2021, 09:48:57 pmOnly 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. ---------------------------------------------------------------------- ASerge · 2021-05-09 21:30 Quote from: lucamar on May 09, 2021, 09:48:57 pmVersion 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. Quote from: 440bx on May 09, 2021, 11:10:14 pmSame with Delphi 2 on Windows. Delphi 10.3 reports "[dcc64 Error] Project1.dpr(36): E2064 Left side cannot be assigned to". ---------------------------------------------------------------------- 440bx · 2021-05-09 21:37 Quote from: ASerge on May 09, 2021, 11:30:43 pmDelphi 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. ---------------------------------------------------------------------- 440bx · 2021-05-11 06:48 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 ? ---------------------------------------------------------------------- Bart · 2021-05-11 12:06 Yes, if only since it is not Delphi compatible. Bart ---------------------------------------------------------------------- PascalDragon · 2021-05-12 07:10 Quote from: 440bx on May 11, 2021, 08:48:01 amNow, 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 · 2021-05-12 08:27 reported.  Ticket : 0038877