Bug 40059

Summary: "uninitialized" warning missed when uninitialized class member used as array index
Product: gcc Reporter: Daniel Pinol <gcc>
Component: middle-endAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: bart.vanassche, fang, gcc-bugs, manu
Priority: P3 Keywords: diagnostic
Version: 4.4.0   
Target Milestone: ---   
Host: Target:
Build: Known to work: 4.8.5
Known to fail: Last reconfirmed:
Bug Depends on:    
Bug Blocks: 24639    

Description Daniel Pinol 2009-05-07 11:25:46 UTC
Compiling this code with "g++-4.4 -O -Wall -g kk.cpp -fdiagnostics-show-option"...
==============
struct S
{
int a;
};

int main()
{
  S s;
  int i;
  bool arr[1];
  arr[s.a] =true;  /*line 11: missed*/  
  arr[i] = true;
/*  if (s.a)
    return 1;
*/

  return 0;
}
==============


produces
kk.cpp:12: warning: ‘i’ is used uninitialized in this function [-Wuninitialized]

It realizes 1 bug, but not the one at line 11 (arr[s.a] =true)


However, if we uncomment the "if (s.a)" it does detect this usage of uninitialized variable

kk.cpp:8: warning: ‘s.S::a’ is used uninitialized in this function [-Wuninitialized]


thanks
Comment 1 Bart Van Assche 2010-02-24 11:46:47 UTC
gcc 4.3.4 doesn't complain about dereferencing uninitialized member variables either.
Comment 2 Manuel López-Ibáñez 2010-02-24 11:58:24 UTC
(In reply to comment #1)
> gcc 4.3.4 doesn't complain about dereferencing uninitialized member variables
> either.
> 

Please, open a new PR, attach a self-contained testcase, add PR 24639 to the block list of the new PR, and add me to the CC list. I will eventually look at it when I find the time.

Thanks.
Comment 3 Richard Biener 2017-03-02 10:23:57 UTC
Works at least since 4.8 and since GCC 5 also with -O0:

> ../../gcc5-g/gcc/cc1plus -quiet t.C -Wuninitialized
t.C: In function ‘int main()’:
t.C:11:10: warning: ‘s.S::a’ is used uninitialized in this function [-Wuninitialized]
    arr[s.a] =true;  /*line 11: missed*/  
          ^
t.C:12:19: warning: ‘i’ is used uninitialized in this function [-Wuninitialized]
      arr[i] = true;
                   ^