Array References Out-of-Bounds

| SimCon Home | fpt Reference Manual Home |

Why is this a Fortran Problem?

Array references out-of-bounds can occur in many languages. They present a particular problem in Fortran because:

The Checks Which Can Be Made

Array references out-of-bounds are always considered to be errors. Two checks may be made:

The Static Check

The fpt command is:

CHECK ARRAY BOUNDS

fpt examines every array index.

If the index is a constant, the value is checked immediately against the declared array indices. This check is also made by all of the compilers we have tested.

If the index is a single variable, program flow is traced backwards to identify every statement where the value of the variable could be assigned. If the assignment is to a constant expression or to a constant range of values in, for example, a DO statement, the value or the range of values is checked against the bounds. Compilers do not, typically make this check.

In either of these cases a diagnostic is issued if the index is out-of-bounds. At present, no static check is made if the array index is a non-constant expression or if the array bounds are variable.

The end of the report from a code used in the design of power generation plant is shown below:

Array Bounds Check ================== : : Line: 8241, File: ... t1/Fortran_codes/pwr_1443/Modified_source/pwr_1443.for pctb1(i) = (avgb1(i)-dthr(i))*100.0/dthr(i) !-------------------------------------^--------------^------------------------- !!! FPT - 3679 Array index is above upper bound !!! FPT - 3679 Array index is above upper bound !------------------------------------------------------------------------------ Line: 8208, File: ... t1/Fortran_codes/pwr_1443/Modified_source/pwr_1443.for bmnpct(i) = (bmean(i)-dthr(i))*100.0/dthr(i) !-----------------------------------------------------^------------------------ !!! FPT - 3679 Array index is above upper bound !------------------------------------------------------------------------------ Line: 8208, File: ... t1/Fortran_codes/pwr_1443/Modified_source/pwr_1443.for bmnpct(i) = (bmean(i)-dthr(i))*100.0/dthr(i) !--------------------------------------^--------------^------------------------ !!! FPT - 3679 Array index is above upper bound !!! FPT - 3679 Array index is above upper bound !------------------------------------------------------------------------------ Total number of array indices checked: 5104 Total number of safe array indices: 3512 Total number which require run-time checks: 1584 Total number with probable bounds violations: 8

 

The Run-Time Check

fpt inserts statements before every executable statement in the code which contains array indices. The statements compare the indices, which may be variables or expressions, with the declared array bounds. A diagnostic is recorded at run-time if the indices are be out-of-bounds, and the occurrences are written to a file when the program terminates.

The command to make the insertion is:

INSERT ARRAY BOUNDS CHECK

The routines which record the reference out-of-bounds are available as source code in the fpt library. The code may be modified to limit the indices to the declared bounds to avoid out-of-bounds references.

Copyright ©1995 to 2024 Software Validation Ltd. All rights reserved.