Diagnosing Fortran Array Bounds Violations Across gfortran, ifort, and nvfortran
Learn how to identify, diagnose, and fix array bounds violations in Fortran programs using gfortran, ifort, and nvfortran. Follow a step‑by‑step guide with compiler flags, sanitizer use, and practical examples.
03 Aug 2025, 10:04 UTC

Recognizable Conditions
When a Fortran program fails with a segmentation fault (SIGSEGV), prints a runtime error such as Array reference out of bounds, or produces incorrect results without crashing, the most likely culprit is an array bounds violation. The symptom may vary depending on the compiler and the platform:
- Crash – often a SIGSEGV or a core dump.
- Runtime error – e.g.,
Runtime error: Array reference out of boundswhen bounds checking is enabled. - Silent corruption – wrong values appear in output or later calculations.
All three major Fortran compilers – gfortran, ifort (Intel LLVM), and nvfortran – provide bounds‑checking options, but the exact flags and diagnostic output differ slightly.
Causes & Diagnostic Table
| Cause | Typical Symptom | Diagnostic Hint |
|---|---|---|
| Explicit index exceeds declared dimension | Crash or runtime error | Check loop bounds; use LBOUND/UBOUND |
| Assumed‑shape dummy argument mismatch | Runtime error when callee expects larger array | Verify caller’s actual size vs dummy’s shape |
| Unallocated pointer/allocatable array accessed | Segfault or garbage data | Check ALLOCATED() before use |
| C interoperability writes past bounds | Crash or data corruption | Confirm C array size and layout matches Fortran expectations |
| Automatic array exceeds stack limit | Segfault or stack‑overflow message | Move to ALLOCATABLE or increase stack size |
| Coarray index out of image bounds | Hang or MPI error | Guard indices with THIS_IMAGE() and NUM_IMAGES() |
Ordered Checks
Enable bounds checking
Recompile with:
# gfortran / nvfortran gfortran -fcheck=bounds -fbacktrace prog.f90 # Intel LLVM (ifort/ifx) ifort -check bounds -traceback prog.f90Run the program again. A bounds error will now point to the offending line.
Add backtrace / traceback
These flags provide a call stack that shows how the offending line was reached, which is crucial if the error occurs in a subroutine called from many places.
Use a sanitizer
Compile with AddressSanitizer to catch heap and stack overflows that bounds checking might miss:
# gfortran / nvfortran gfortran -fsanitize=address -fno-omit-frame-pointer prog.f90 # Intel LLVM ifort -fsanitize=address -fno-omit-frame-pointer prog.f90Run under
ASAN_OPTIONS=detect_stack_use_after_return=1to catch post‑deallocation use.Validate caller‑callee interfaces
For assumed‑shape arguments, ensure the caller provides an explicit interface (module or interface block) and that the actual array’s bounds satisfy the dummy’s shape. Missing interfaces can silence bounds errors.
Check allocation status
Before accessing a pointer or allocatable array, insert:
if (.not. ALLOCATED(ptr)) then print *, "Pointer not allocated" stop end ifReview OpenMP/coarray regions
When using SIMD or parallel directives, speculative loads can cross array ends. Use compiler‑specific pragmas (e.g.,
!$OMP SIMD SAFELEN) or disable vectorization for critical loops.
Fixes Tied to Findings
- Incorrect loop bounds – Replace hard‑coded limits with
LBOUND(array)orUBOUND(array). - Assumed‑shape mismatch – Change dummy to
(:)or provide an explicit interface that matches the caller’s actual bounds. - Unallocated array – Add
ALLOCATE(array(n))orALLOCATE(array, SOURCE=initial)before first use. - C interop overflow – Use
ISO_C_BINDINGto bind C pointers and verify the C side’s size. Convert Fortran arrays to C contiguous order withcolumn-majorvsrow-majorawareness. - Stack overflow – Replace large automatic arrays with
ALLOCATABLEor increase the OS stack limit withulimit -s. - Coarray index out of bounds – Guard with
if (idx <= UBOUND(array,1) .and. idx >= LBOUND(array,1))and useTHIS_IMAGE()to avoid out‑of‑image accesses.
Escalation Criteria
| Condition | Escalate To |
|---|---|
| Bounds checking passes but sanitizer reports a heap overflow | Suspect ABI mismatch or compiler bug; file minimal reproducible example with vendor. |
| Silent corruption persists across compilers | Introduce assertions on indices; review algorithm logic. |
| Stack overflow only occurs at large problem sizes | Redesign memory layout; consider module‑level ALLOCATABLE arrays or SAVE attribute. |
| Coarray errors manifest as hangs | Enable -fcoarray=lib for better diagnostics or switch to MPI if appropriate. |
Practical Example
Below is a minimal program that deliberately writes past the end of an array. Compile with bounds checking and observe the error.
program out_of_bounds
implicit none
integer, dimension(5) :: a
integer :: i
do i = 1, 6 ! Off‑by‑one bug
a(i) = i
end do
end program
Compile:
# gfortran -fcheck=bounds -fbacktrace prog.f90 -o prog
Run:
$ ./prog
Runtime error: Array reference out of bounds
at line 6 column 5 of file out_of_bounds.f90
at line 6 column 5 of file out_of_bounds.f90
at line 6 column 5 of file out_of_bounds.f90
... (stack trace)
Notice the line number and file name. If you had a subroutine, the backtrace would show the chain of calls that led to the error.
Limitations & Practical Checks
- Bounds checking only detects violations that actually occur at runtime; it will not catch logic errors where indices stay within bounds but are semantically wrong.
- Assumed‑size arrays (
(*)) and assumed‑rank arrays ((..)) disable bounds checking for the affected dimension. Prefer assumed‑shape ((:)) with an explicit interface. - Automatic arrays in recursive procedures can overflow the stack before bounds checking triggers. Test with
ulimit -sand move large arrays toALLOCATABLEif needed. - Coarray diagnostics may be less clear on older compilers; use
-fcoarray=libfor enhanced error messages. - On GPU offload (nvfortran), bounds checking is available for host code but may require
-Mboundson device kernels.
To verify that a fix works, recompile with bounds checking enabled and confirm that the program no longer crashes and that the output matches expected values. For silent corruption, add runtime assertions or print statements to confirm data integrity.
Compiler‑Specific Notes
- gfortran –
-fcheck=allenables bounds, do‑loop, and memory checks. Use-fbacktracefor stack traces. - Intel LLVM (ifort/ifx) –
-check allcovers bounds, format, and output conversion.-tracebackprovides a stack trace. - nvfortran –
-Mboundsactivates host bounds checking;-Minfo=allgives additional diagnostics.
Bounds checking typically incurs a 2–10× slowdown. In production builds, disable it via build profiles and rely on static analysis and unit tests to catch the same errors earlier.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.