SAL
SAL

Reputation: 1260

Array bounds checking in Fortran - is this dangerous?

I have some legacy Fortran code which I'm recompiling using the latest Intel compiler for Windows (that runs within Visual Studio). Sadly my Fortran skills are more than a little rusty.

This particular code makes use of an array to note any problems that occur during the program's execution. This array is declared thus:

CHARACTER FAIL(25)*(255)

Here's a piece of code that checks this array and does something if it finds an element beginning with 'F' (ie 'failed')

      DO 20 CNTERR = OERRNO,ERRNO
         IF (FAIL(CNTERR)(1:1).EQ.'F') GOTO 9999
   20 CONTINUE

In almost all cases OERRNO and ERRNO are zero, and thus CNTERR will also be zero. Which means, I think, that - as Fortran uses 1-based arrays - I'm looking at an element which doesn't exist.

The code will compile and run without any problems using the default 'Release' Configuration within VS. It will compile but fail to run with the default 'Debug' Configuration. The important difference, I think, is that 'Debug' has the 'Check Array and String Bounds' option set ie /check:bounds.

My question: The code appears to run correctly if built with the 'Release' configuration. But am I doing a seriously Bad Thing that will bite me? What does Fortran return when you ask for an out-of-bounds array element?

Upvotes: 2

Views: 2759

Answers (1)

Ian Bush
Ian Bush

Reputation: 56

My question: The code appears to run correctly if built with the 'Release' configuration. But am I doing a seriously Bad Thing that will bite me? What does FORTRAN return when you ask for an out-of-bounds array element?

You are right. Accessing out of bounds is always wrong, and yes it could easily bite you. As to what Fortran says will happen? Well it merely says you can't do it, and in such cases the implementation can do anything it likes, from nothing, to outputting a useful error message, to sounding the 3 minute warning.

To fix it we need more code, but I suspect initialising OERRNO to 1 will do it, as then in the case ERRNO=0 the loop will not be executed.

One other thing:

CHARACTER FAIL(25)*(255)

My F77 is rusty, F90 style is different here, but I'm not sure this is right. My failing memory suggests

CHARACTER*255 FAIL(25)

instead.

Upvotes: 4

Related Questions