Forum > General

reading from out of array bounds

<< < (2/2)

Handoko:
I prefer it off by default. Because it has significant impact on performance on certain types of programs, for example games.

440bx:

--- Quote from: PascalDragon on April 22, 2021, 01:34:24 pm ---The default is off, because there is legitimate code that uses this behaviour,

--- End quote ---
That range checking is off by default is fine.  What is NOT fine is that when a constant that is out of bounds is used, the compiler should NOT accept that because it knows it is not just a bounds violation, it is also a data type violation.

Of course, it's a different story when using variables, because obviously the compiler cannot know if a variable is out of bounds at compile time, which is implicit in the example you provided but, that example doesn't apply at compile time because it uses variables instead of constants.


--- Quote from: PascalDragon on April 22, 2021, 01:34:24 pm --- 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.

--- End quote ---

Anyway, I know this is not going to change (unfortunately) but, for the OP, a _correctly_ implemented Pascal compiler would NOT accept a constant index out of the array bounds and, as an example, Delphi refuses it (as it should.)

Thaddy:

--- Quote from: 440bx on April 22, 2021, 02:25:12 pm --- Delphi refuses it (as it should.)

--- End quote ---
Not correct. At least D7 and XE2 [edit: same with D10.1] accept it (The array[0..0] construct). But a warning is given. Delphi uses similar constructs like PascalDragon described.
Furthermore you can not simply state that Delphi accepts it without referring to a version. That is the same in FreePascal: explanation follows from version. Leaving the version out leads to noice.

If possible, use a custom range. In that case both FPC and Delphi catch the error at compile time given a compile time const.

(Note that the real issue is that you can cause stack problems)

440bx:

--- Quote from: Thaddy on April 22, 2021, 02:35:37 pm ---Not correct. At least D7 and XE2 [edit: same with D10.1] accept it (The array[0..0] construct). But a warning is given. Delphi uses similar constructs like PascalDragon described.
Furthermore you can not simply state that Delphi accepts it without referring to a version. That is the same in FreePascal: explanation follows from version. Leaving the version out leads to noice.

If possible, use a custom range. In that case both FPC and Delphi catch the error at compile time given a compile time const.

(Note that the real issue is that you can cause stack problems)

--- End quote ---
Sometimes I think you post nonsense just to start an argument even though you know beforehand you are wrong.

The OP showed this code :
--- 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";}};} ---var  a : array [0..5] of integer; begin   writeln(a[15]);end.and Delphi will NOT compile that. 

There is one thing you are correct in, I have not tested it in every version of Delphi (nor do I intend to) but, I very strongly doubt that any version would compile that _incorrect_ program that FPC _incorrectly_ accepts.

As I stated in the previous post, when using _constants_ and, FYI, in a[15] the numeral 15 is a _constant_ and it is out of the array bounds, the compiler should summarily reject it.  No correctly implemented Pascal compiler would accept that and, the reason is because the out of bounds constant represents a _data type_ violation.

pascal111:
Can this checking help:
(untested)


--- 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";}};} ---    var      a : array [0..5] of integer;      x:integer;    begin              x=15;              if (x>=low(a)) and (x<=high(a)) then       writeln(a[x]);     end. 

Navigation

[0] Message Index

[*] Previous page

Go to full version