`gfortran-14`: "MEM <real(kind=4)[0:]> [(real(kind=4)[0:] *)_32][0]’ may be used uninitialized"

When playing with gfortran-14.2.0, I encountered an issue with -Wmaybe-uninitialized as illustrated below.

$ uname -a && gfortran --version && gfortran -Werror -Wuninitialized -fsanitize=undefined -O2 test_uninit.f90 
Linux 6.14.0-27-generic #27~24.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue Jul 22 17:38:49 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
GNU Fortran (Ubuntu 14.2.0-4ubuntu2~24.04) 14.2.0
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

test_uninit.f90:36:19:

   36 | f = [(0.0, i=1, k)]
      |                   ^
Error: ‘MEM <real(kind=4)[0:]> [(real(kind=4)[0:] *)_32][0]’ may be used uninitialized [-Werror=maybe-uninitialized]
f951: all warnings being treated as errors

Here, test_uninit.f90 is the code below (see also test_compiler/test_uninit.f90 at master · zequipe/test_compiler · GitHub).

!--------------------------------------------------------------------------------------------------!
! test_uninit.f90
!
! This test illustrates a bug in gfortran 14. With
! ```
! gfortran -Werror -Wuninitialized -fsanitize=undefined -O2 test_uninit.f90
! ```
! or
! ```
! gfortran -Werror -Wmaybe-uninitialized -fsanitize=undefined -O2 test_uninit.f90
! ```
! it fails with:
! ```
! test_uninit.f90:36:19:
!
!    36 | f = [(0.0, i=1, k)]
!       |                   ^
! Error: ‘MEM <real(kind=4)[0:]> [(real(kind=4)[0:] *)_32][0]’ may be used uninitialized [-Werror=maybe-uninitialized]
! f951: all warnings being treated as errors
! ```
! Even with `Wuninitialized`, the error is still `-Werror=maybe-uninitialized`.
! The same error occurs with `-O3`, `-O4`, and `-Ofast`, but not with `-O` `-O0`, or `-O1`.
!--------------------------------------------------------------------------------------------------!
module test_mod
implicit none
private
public :: test

contains

subroutine test(k)
implicit none
integer(8), intent(in) :: k
integer(8) :: i
real :: f(k)
f = [(0.0, i=1, k)]
f = 0.0
end subroutine test
end module test_mod

program test_program
end program test_program

Any idea why this happens? Thanks.

N.B

  1. This example is minimal. Any change to the code or the compiler flags will make the error disappear.
  2. Surely, integer(8) is not standard-conforming. I wrote it in this way because gfortran is the only compiler concerned. I would not write this kind of code in production.
  3. Even with -Wuninitialized, the error still says “-Werror=maybe-uninitialized”.
1 Like

Looks like a false positive

A guess: because of second initialization f = 0.0, compiler may be optimizing out the first initialization. Once the first initialization is optimized out, i will not be used and will not be initialized.

FWIW, the above page has a comment like this (at the end of the -fsanitize=undefined section):

Note that sanitizers tend to increase the rate of false positive warnings, most notably those around -Wmaybe-uninitialized. We recommend against combining -Werror and [the use of] sanitizers.

I’ve also experienced similar warnings several times, but in my cases they had no actual problems in the computed results (but of course, YMMV). So I often attached -Wno-maybe-uninitialized to suppress those warnings. It may be useful to compare outputs with other versions (including newer ones) if possible.

2 Likes

The same error occurs with gfortran 14.1.0, 14.2.0, 14.3.0, 15.1.0, and 15.2.0, but not with 13.4.0 or lower.

This is a reasonable guess. However, the real reason seems more unreasonable, because the following code encounters the same error.

!--------------------------------------------------------------------------------------------------!
! test_uninit.f90
!
! This test illustrates a bug in gfortran 14. With
! ```
! gfortran -Werror -Wuninitialized -fsanitize=undefined -O2 test_uninit.f90
! ```
! or
! ```
! gfortran -Werror -Wmaybe-uninitialized -fsanitize=undefined -O2 test_uninit.f90
! ```
! it fails with:
! ```
! test_uninit.f90:36:19:
!
!    36 | f = [(0.0, i=1, k)]
!       |                   ^
! Error: ‘MEM <real(kind=4)[0:]> [(real(kind=4)[0:] *)_32][0]’ may be used uninitialized [-Werror=maybe-uninitialized]
! f951: all warnings being treated as errors
! ```
! Even with `Wuninitialized`, the error is still `-Werror=maybe-uninitialized`.
! The same error occurs with `-O3`, `-O4`, and `-Ofast`, but not with `-O` `-O0`, or `-O1`.
!--------------------------------------------------------------------------------------------------!
module test_mod
implicit none
private
public :: test

contains

subroutine test(k)
implicit none
integer(8), intent(in) :: k
integer(8) :: i
real :: f(k), x(k)
f = [(0.0, i=1, k)]
x = 0.0
end subroutine test
end module test_mod

program test_program
end program test_program