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

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Free Pascal - Lazarus mailing list
Hi,

Apologies if this email has been sent to the wrong location (I'm a brand
new user to the Lazarus emailing list).

I recently had a software/compilation bug whereby no exception was
raised when my code was accessing an array outside of its range. 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
--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

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

Free Pascal - Lazarus mailing list
On Mon, 1 May 2017 11:53:55 +1200
Graham Ward via Lazarus <[hidden email]> 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
--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

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

Free Pascal - Lazarus mailing list
In reply to this post by Free Pascal - Lazarus mailing list


On Mon, 1 May 2017, Graham Ward via Lazarus wrote:

> Hi,
>
> Apologies if this email has been sent to the wrong location (I'm a brand
> new user to the Lazarus emailing list).
>
> 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?

The Free Pascal compiler does not do range checking by default, you need to
enable it  with -Cr (there is an option in the project option dialog).

araminta: ~ >fpc ta.pp -Cr
araminta: ~ >./ta
Runtime error 201 at $00010D50
   $00010D50
araminta: ~ >cat ta.pp

Var
   I : Integer;
   A : Array of byte;
   B :  Array of byte;

begin
   SetLength(A,12);
   SetLength(B,12);
   for I:=0 to length(A)+1 do
     A[i]:=1;
end.

Michael.
--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus-ide.org/listinfo/lazarus