Forum > General

Unexpected FPC v3.0.4 Behavior with WRITEABLECONST OFF

(1/2) > >>

440bx:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$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:
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:

--- Quote from: lucamar on May 09, 2021, 09:48:57 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.

--- End quote ---
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:

--- Quote from: 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.

--- End quote ---
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 pm ---Same with Delphi 2 on Windows.

--- End quote ---
Delphi 10.3 reports "[dcc64 Error] Project1.dpr(36): E2064 Left side cannot be assigned to".

440bx:

--- Quote from: ASerge on May 09, 2021, 11:30:43 pm ---Delphi 10.3 reports "[dcc64 Error] Project1.dpr(36): E2064 Left side cannot be assigned to".

--- End quote ---
Thank you for reporting that Serge.  Nice that they fixed that.

Navigation

[0] Message Index

[#] Next page

Go to full version