PRC01-F. Disable the implicit declaration of procedures
Program units should explicitly disable the implicit declaration of procedures using implicit none(external) (or the stricter implicit none(type, external) ), or by ensuring all referenced procedures have explicit interfaces.
In Fortran, a procedure called without an explicit interface is treated as having an implicit interface . The compiler makes assumptions about the characteristics of arguments (types, ranks, etc.) and function return types. This prevents compile-time verification that the actual arguments match the dummy arguments, potentially causing undefined behavior at runtime if mismatches occur.
The Fortran 2018 standard introduced implicit none(external) , which enforces that every external and dummy procedure in the scoping unit must either have an explicit interface or be explicitly declared with the external attribute.
Noncompliant Code Example
In this noncompliant example, factorial is compiled separately. The main program does not have an explicit interface for factorial . Because implicit none(external) is omitted, the compiler assumes factorial is a valid external procedure with an implicit interface. Passing a REAL variable to a subroutine expecting an integer leads to undefined behavior at runtime.
! external procedure defined elsewhere
subroutine factorial(n, res)
implicit none
integer, intent(in) :: n
integer, intent(out) :: res
!... code implementation here ...
end subroutine factorial
program math_test
implicit none
integer :: n = 5
real :: r ! Mismatch: 'r' is REAL, but 'factorial' expects integer
! Noncompliant: Implicit declaration of procedure 'factorial'
call factorial(n, r)
end program math_test
Compliant Solution
The compliant solution uses implicit none(type, external) . While implicit none(external) is sufficient to prevent implicit procedure interfaces, including type is recommended to also disable implicit typing of variables, providing stricter compile-time checking.
! external procedure defined elsewhere
module math_mod
implicit none
contains
subroutine factorial(n, res)
integer, intent(in) :: n
integer, intent(out) :: res
!... code implementation here ...
end subroutine factorial
end module math_mod
program math_test
use math_mod, only: factorial
! Compliant: Requires all procedures to have explicit interfaces
implicit none (type, external)
integer :: n = 5
integer :: r ! Correct type
call factorial(n, r)
end program math_test
Risk Assessment
Implicit procedure declarations disable compile-time argument checking. This significantly increases the risk of passing arguments with the wrong type, rank, or number, resulting in undefined behavior that may go undetected until runtime.
| Recommendation | Severity | Likelihood | Detectable | Repairable | Priority | Level |
| PRC01-F | High | Likely | Yes | No | P18 | L1 |
Attachments:
button_arrow_left.png (image/png)
button_arrow_up.png (image/png)
button_arrow_right.png (image/png)


