Forum > General

reading from out of array bounds

(1/2) > >>

inky:
i think i remember that this kind of code was not possible to compile:


--- Code: ---var
  a : array [0..5] of integer;

begin
   writeln(a[15]);
end.

--- End code ---

did something change or broke? or do i remember incorrectly?
then why is this legal?

Zvoni:
?
https://www.freepascal.org/docs-html/prog/progsu65.html

--- Quote ---By default, the compiler doesn’t generate code to check the ranges of array indices, enumeration types, subrange types, etc.

--- End quote ---

wp:

--- Quote from: inky on April 21, 2021, 02:47:55 pm ---did something change or broke?

--- End quote ---
In the default installation of Lazarus the compilation checks (range check, overflow, etc) are off. Therefore your code passes compilation (although a warning is given "project1.lpr(6,14) Warning: Range check error while evaluating constants (15 must be between 0 and 5)". However, when you turn Range Checking on ("Project Options" > "Compiler options" > "Debugging"), the code is no langer accepted.

inky:
thank you!

i was expecting that there should be a runtime check and compile time check.

and i was thinking that the compile time check is necessary because of the language standard. but i understand now that it's not even clear if there is such a language standard, because these delphi/objfpc dialects are not standardized.

and now we have to rely on compiler and compiler checks. i am surprised that the compiler is not "obliged" to interrupt the compilation, but that depends on the switch.

so practically we have the implementation that defines the standard, not vice versa.

PascalDragon:

--- Quote from: inky on April 22, 2021, 01:29:57 pm ---and now we have to rely on compiler and compiler checks. i am surprised that the compiler is not "obliged" to interrupt the compilation, but that depends on the switch.
--- End quote ---

The default is off, because there is legitimate code that uses this behaviour, especially when interfacing with code like the Windows API:


--- 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";}};} ---   tagSAFEARRAY = record     cDims: USHORT;     fFeatures: USHORT;     cbElements: ULONG;     cLocks: ULONG;     pvData: PVOID;     rgsabound: array[0..0] of SAFEARRAYBOUND;   end;   TSafeArray = tagSAFEARRAY;   SAFEARRAY = TSafeArray; 
This is from the ActiveX unit. With bounds checks enabled one wouldn't be able to access all elements of rgsabound.

Navigation

[0] Message Index

[#] Next page

Go to full version