User account creation filtered due to spam.

Bug 47307 - Uninitialized in this function: warning for initialized, no warning for uninitialized
Summary: Uninitialized in this function: warning for initialized, no warning for unini...
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.6.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2011-01-15 15:59 UTC by Jinsong Zhao
Modified: 2012-09-21 18:56 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jinsong Zhao 2011-01-15 15:59:55 UTC
Hi there,

With the example code below, gfortran (4.5.0 and 4.6.0) give a strange warning message with -O2 flag:

> gfortran -Wall -O2 -c readmo.f90
readmo.f90: In function 'readmo':
readmo.f90:4:0: warning: 'convrt' may be used uninitialized in this function

When with -O0, the warning message disappear.

In fact, the array 'lopt' is not initialized in the code, however, the compiler does not give warning about that. I try the same code with g95, it give a warning message 'used but not set' about 'lopt'.

Any suggestions or comments? Thanks in advance!

Regards,
Jinsong



subroutine readmo()
!!!! example code readmo.f90 !!!!

   implicit none
   real :: convrt
   integer :: i, iflag, j
   integer, dimension(3,5) :: lopt

   iflag = 0
   do i = 1, 5
      do j = 1, 3
         if ( lopt(j,i) < 0 ) then
            convrt = 1.0
            if ( j > 1 ) convrt = 2.0
            iflag = 1
         endif
      enddo
   enddo
   if ( iflag /= 0 ) then
      do i = 1, 5
         write(6,*) i*convrt
      enddo
   endif

end subroutine readmo
Comment 1 Tobias Burnus 2011-01-15 16:22:27 UTC
Move to middle end. From the dump:

readmo ()
{
  integer(kind=4) lopt[15];
  real(kind=4) convrt;
  [...]
  iflag = 0;
  [...]
    if (lopt[((integer(kind=8)) j + (integer(kind=8)) i * 3) + -4] < 0)
      {
        convrt = 1.0e+0;
        iflag = 1;
      }
  if (iflag != 0)
    ... access convrt ...

Thus, lopt is never set, but not warned for. But convrt is only accessed if it is set. However, the middle-end (with optimization) only has
  warning: 'convrt' may be used uninitialized in this function
Comment 2 Dominique d'Humieres 2011-01-15 21:50:05 UTC
> In fact, the array 'lopt' is not initialized in the code, however, the compiler
> does not give warning about that. I try the same code with g95, it give a
> warning message 'used but not set' about 'lopt'.

Add lopt(1,1) = 0 at the beginning of your code and the g95 warning just disappears!
So don't rely too much of this kind of warning.

> Any suggestions or comments? Thanks in advance!

Searching bugzilla for "uninitialized" in the subject yields 52 entries for open pr (not counting those closed as INVALID, DUPLICATE, WONTFIX, ...). I let you browse the list to check if this pr is a duplicate of one of those (look for instance to pr24639, my choice is pr27120).

As far as I understand the problem, 
(1) it could be as difficult to solve as running the code itself (think of an array of 10**9 elements set through a spaghetti code and forgetting to set only one element). Note this apply also to duplicate initialization (as in Fortran legalese;-).
(2) the responsibility to use only set variables is on the user, not on the compiler.