Bug 46684

Summary: GCC throws incorrect "may be used uninitialized" warnings
Product: gcc Reporter: William Witt <william>
Component: cAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: normal CC: manu
Priority: P3    
Version: 4.5.0   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:

Description William Witt 2010-11-27 15:12:06 UTC
Originally reported: https://bugzilla.novell.com/238855

% cat x2.c
enum BLA {A, C};

int f (enum BLA a, int i)
{
  int ret, ret2;
  switch (a) {
    case A:
      //ret2=42;
      if (i) ret=1; else ret=3;
      break;
    case C:
      ret = 2;
      ret2=1;
      break;
  }
  if (ret2 || ret)
    return 42;
  return 43;
}

(note the missing initialization of ret2 in the A arm).  Now it doesn't
warn at all:
% gcc -c -W -Wall -O2 x2.c
%

Now, we can change it a bit more, and get "may be" warnings:

% cat x3.c
enum BLA {A, B, C};

int f (enum BLA a, int i)
{
  int ret, ret2;
  switch (a) {
    case A: case B:
      //if (i) ret=1; else ret=3;
      ret = 1;
      ret2=42;
      break;
    case C:
      ret = 2;
      ret2=1;
      break;
  }
  if (i || ret || ret2)
    return 42;
  return 43;
}

Note the missing 'if' and the changed second if condition.  It's now:

% gcc -c -W -Wall -O2 x3.c
x.c: In function ‘f’:
x.c:5: warning: ‘ret2’ may be used uninitialized in this function
x.c:5: warning: ‘ret’ may be used uninitialized in this function
Comment 1 Richard Biener 2010-11-27 18:14:45 UTC
As only constant initializers are involved CCP optimizes based on undefinedness
of uninitialized variables.
Comment 2 Manuel López-Ibáñez 2012-10-24 23:52:06 UTC
(In reply to comment #1)
> As only constant initializers are involved CCP optimizes based on undefinedness
> of uninitialized variables.

Ergo, bug 18501.

*** This bug has been marked as a duplicate of bug 18501 ***