-fcheck, -g and -fbacktrace

An array reference outside its bounds is not a compile error and usually not a crash either. The program reads whatever lies next in memory, prints a number and exits with status zero, and nothing in the output says that the number is meaningless. gfortran can check every subscript at run time, and the three flags of this lesson turn that check on, put the source lines into the message and decide how much of a backtrace follows it.

The bug that does not crash

The program fills an array of five values and sums it with a loop whose last index is one too large. The bound arrives from a variable, which is the ordinary case in real code: n counts something and the count is wrong by one.

The file bounds2.f90:

program bounds2 implicit none integer :: a(5), i, n, total a = [10, 20, 30, 40, 50] n = 5 n = n + 1 total = 0 do i = 1, n total = total + a(i) end do print '(a,i0)', 'total = ', total end program bounds2

gfortran -Wall -Wextra -std=f2018 bounds2.f90 -o bounds
./bounds

total = 150

The run is clean and the answer is right, by luck: the word after the array happened to hold zero. The sixth value is not part of the array, so the sum is formally undefined and may be 150 today, another number tomorrow and a segmentation fault on a different compiler. This is the worst kind of bug, because every signal a build or a run gives is green.

A literal bound is the one case the compiler itself can see. Change line 7 to do i = 1, 6 and a plain syntax check reports:

gfortran -std=f2018 -fsyntax-only bounds.f90

bounds.f90:8:22:

   7 | do i = 1, 6
      |             2
   8 |    total = total + a(i)
      |                      1
Warning: Array reference at (1) out of bounds (6 > 5) in loop beginning at (2)

That warning needs no flag, and it disappears the moment the bound comes from a variable, which is why a test build does not rely on the compiler for this class of mistake.

-fcheck=all

-fcheck=all adds a test in front of every subscript, and the run stops at the first one that is out of range. The source does not change:

gfortran -Wall -Wextra -std=f2018 -g -fcheck=all bounds2.f90 -o bounds_check
./bounds_check

At line 10 of file bounds2.f90
Fortran runtime error: Index '6' of dimension 1 of array 'a' above upper bound of 5

Error termination. Backtrace:
#0  0x79717a1f8430 in ???
#1  0x79717a1f9076 in ???
#2  0x79717a1f963b in ???
#3  0x5dd7cbaf732a in bounds2
  at /tmp/w114/bounds2.f90:10
#4  0x5dd7cbaf7474 in main
  at /tmp/w114/bounds2.f90:13

The message names the index, the dimension of the array and the bound it broke, and the status is 2 rather than 0, so a shell script, a Makefile or a test runner sees the failure. -fcheck=all is a group of checks, and the one that fired here is bounds; the same switch also covers array-temps, do, mem, pointer and recursion, and -fcheck=bounds alone gives the message above.

-g and the lines

The frames with names and source lines come from -g. Without it the same binary prints the same first three lines and a list of addresses:

gfortran -Wall -Wextra -std=f2018 -fcheck=all bounds2.f90 -o bounds_nog
./bounds_nog

At line 10 of file bounds2.f90
Fortran runtime error: Index '6' of dimension 1 of array 'a' above upper bound of 5

Error termination. Backtrace:
#0  0x71ed5128b430 in ???
#1  0x71ed5128c076 in ???
#2  0x71ed5128c63b in ???
#3  0x568167f7e32a in ???
#4  0x568167f7e474 in ???
#5  0x71ed51611153 in libc_start_main_stage2
  at src/env/__libc_start_main.c:95

The At line 10 line is part of the check and appears either way, because the check itself knows the line. What -g adds is the frame table: without it, the two frames that belong to the program are addresses with no name, and a crash inside a procedure would name nothing at all. A test build therefore carries -g -fcheck=all together, and the pair costs nothing at run time that the check does not already cost.

-fbacktrace

The trace after the message is on by default on this target, and two switches turn it off: the compile flag -fno-backtrace, and the variable GFORTRAN_ERROR_BACKTRACE at run time, which needs no rebuild:

GFORTRAN_ERROR_BACKTRACE=0 ./bounds_check

At line 10 of file bounds2.f90
Fortran runtime error: Index '6' of dimension 1 of array 'a' above upper bound of 5

The status is still 2 and the message still names the line, so a test log keeps what it needs and loses nine lines of addresses that change on every run. The -fno-backtrace build prints exactly these two lines as well. A program that is being debugged keeps the trace, a program that is being tested in a loop sets the variable, and a page like this one quotes the trace once and uses the short form afterwards.

Rules for a test build

Keep two builds of the same source: a release build with the optimisation the program needs, and a test build with -g -fcheck=all -ffpe-trap=invalid,zero,overflow -fbacktrace. Run the tests against the test build, so that a bad subscript fails a test instead of corrupting a result, and keep the release build for the numbers that are timed. The next lesson is the floating-point half of that line: the traps that stop a program on a division by zero or a NaN, where -fcheck does not look.

One limit is worth knowing before the check is trusted. -fcheck=bounds tests the subscript against the array it is applied to, so it finds an index past the end of an explicit-shape array, an assumed-shape dummy and an allocatable. It does not find a wrong length passed as a separate argument, an array that is walked through a(1:n) with a wrong n, or an access through a pointer: these are still the programmer's responsibility, and the check is a net with holes, not a proof.

Exercises

  1. Change the loop to do i = 1, 5 and run the -fcheck=all build. What does it print now, and what does the status say?
  2. Build with -fcheck=bounds instead of -fcheck=all. Is the message the same?
  3. Move the loop into a subroutine with a dummy a(:) and call it with the whole array. Which bound does the message name, and why is it the same number?
  4. Run ./bounds_check; echo $? in a shell. Which status does the shell report, and what does echo $? print after the un-checked build?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does -fcheck=all add to a program?

    Answer

    Run-time tests for subscripts, pointers and a few related conditions

    Bounds, array temporaries, DO variables, memory, pointers and recursion.

  2. What is the exit status of the unchecked bounds2 run?

    Answer

    0, because the out-of-bounds read is not tested

    It printed total = 150 and reported success.

  3. What does the checked run print?

    Answer

    Fortran runtime error: Index '6' of dimension 1 of array 'a' above upper bound of 5

    The message names the index, the dimension and the bound.

  4. Which flag puts procedure names and source lines into the backtrace?

    Answer

    -g

    The check knows the line; -g gives the frames.

  5. How do you turn the backtrace off without rebuilding?

    Answer

    GFORTRAN_ERROR_BACKTRACE=0 before the run

    -fno-backtrace does it at build time.

  6. Why is the unchecked answer 150 a bad sign rather than a good one?

    Answer

    The sixth element is outside the array, so any value is possible

    The word after the array happened to be zero.

  7. When does the compiler warn about an out-of-bounds subscript by itself?

    Answer

    When the bound is a constant it can see at compile time

    A bound from a variable is invisible to the compiler.

  8. Which flag checks only the subscripts?

    Answer

    -fcheck=bounds

    -fcheck=all is the whole group.

  9. What does the test build look like?

    Answer

    -g -fcheck=all -ffpe-trap=invalid,zero,overflow -fbacktrace

    Checks and debug information, no optimisation of the values.

  10. Which bug does -fcheck=bounds not find?

    Answer

    A wrong length passed as a separate argument beside the array

    The subscript is checked against the array, not against your intention.

The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev