[Lazarus] Lazarus Bug: No exception raised when attempting to access an array outside of range.

Mattias Gaertner nc-gaertnma at netcologne.de
Mon May 1 09:04:44 CEST 2017


On Mon, 1 May 2017 11:53:55 +1200
Graham Ward via Lazarus <lazarus at lists.lazarus-ide.org> wrote:

>[...]
> I recently had a software/compilation bug whereby no exception was 
> raised when my code was accessing an array outside of its range.

Enable runtime range checks:
Project / Project Options / Compiler Option / Debugging / Checks / Range

Or have a "debug" and "release" mode:
http://wiki.lazarus.freepascal.org/IDE_Window:_Compiler_Options#Adding_a_release_and_debug_build_modes

Mattias


 Took 
> some detective work, as it was resulting in corruption at runtime with 
> completely unrelated code. This was caused by a bug in my own software 
> that tried to access index 72 for an array of range [0..71]. What was 
> more interesting was that if I expanded the array to 72 (or 73,74,75 for 
> that matter) and then tried to access range+1 (IE index 73 for the array 
> with an index of [0..72] this appeared to cause no ill effect to my 
> program once more, so I was extremely "lucky" to have such an obvious 
> bug appear in my code. Something more subtle would have been a lot more 
> difficult to locate.
> 
> The offending code is as follows:
> 
>   setlength(isMerged, ParityCol);     // fixed length for array keeping 
> track of merged cells.
>   setlength(ColIndex, ParityCol);     // Define the length of the Column 
> Index
>   For J := 0 to ParityCol do
>     Begin
>       isMerged[J] := False; //This line was leading to the program 
> becoming unresponsive in another totally unrelated part of code 
> (AssignFile...) in another procedure
>       ColIndex[J] := 1000; // This line, although also accessing outside 
> of the legal range, did not appear to cause any problems!!!
>     end;
> 
> Note I even tried the following line, which is obviously wrong (as the 
> array has a maximum index of 71)
> 
>     ColIndex[175] := 500;
> 
> In other languages (that I am more familiar with) an exception (Error : 
> Array index outside of range) would have been raised. Why does the 
> Lazarus compiler not raise a similar error?
> 
> I am using:
> 
> Code Typhon Version 6.0
> FPC Version 3.1.1
> SVN Revision : 54036
> 
> 
> Assuming this is an actual bug in the compiler, then I hope this 
> information proves useful. If not, then please advise me accordingly, 
> thanks.
> 
> Aside from this, let me say a very big thank you to you guys for all 
> your hard work in what is a very useful piece of software for me (FPC). 
> Your efforts are greatly appreciated.
> 
> Kind Regards,
> Graham
> New Zealand


More information about the Lazarus mailing list