Warning: ‘.str’ may be used uninitialized

Code:

! test_char.f90
program test_char
implicit none
character(len=:), allocatable :: str
str = 'hello world'
end program test_char

Test:

$ gfortran --version && gfortran -g -Wall -std=f2018 test_char.f90

GNU Fortran (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0

test_char.f90:9:19:

    9 | str = 'hello world'
      |                   ^
Warning: ‘.str’ may be used uninitialized [-Wmaybe-uninitialized]

Questions:

  1. Is the code standard-conforming?
  2. Why str is considered maybe uninitialized?
  3. What is the dot preceding str?

Remarks

  1. I am aware that -Wall implies -Wmaybe-uninitialized. What interests me is not why this option is turned on or whether it should be, but rather why the code triggers this warning.
  2. The following compilation finishes without warning.
	ifort -g -warn all -check all -debug extended test_char.f90
	ifx -g -warn all -check all -debug extended test_char.f90
	nagfor -g -C test_char.f90
	flang -g -Wall -Wextra test_char.f90
	nvfortran -g -Wall -Wextra test_char.f90
	aocc-flang -g -Wall -Wextra test_char.f90
	sunf95 -g -w3 -ansi -xcheck=%all -C test_char.f90

Thanks.

It seems it’s a gfortran issue 91442 – Wrong "may be used uninitialized" warning with allocation on assignment
(or even early 56670 – Allocatable-length character var causes bogus warning with -Wuninitialized)

As mentioned there the addition of optimization key e.g. ‘-O2’ removes this warning.

1 Like

Thank you @band-a-prend for the information.

A related thread was Gfortran uninitialized warnings. These warnings of gfortran are sometimes spurious, so I turn them off.

1 Like

With copy-paste of your code and a more recent / contemporary release of gfortran 12.2.0 as provided by Linux Debian 12/bookworm (currently branch testing), I do not observe an error warning. To quote the CLI

$ gfortran --version && gfortran -g -Wall -std=f2018 test_char.f90
GNU Fortran (Debian 12.2.0-14) 12.2.0
Copyright (C) 2022 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.


2 Likes

Thank you @nbehrnd . The same on my side. It is great to see the bug fixed in gfortran-12.