Bug 46684 - GCC throws incorrect "may be used uninitialized" warnings
Summary: GCC throws incorrect "may be used uninitialized" warnings
Status: RESOLVED DUPLICATE of bug 18501
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.5.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-11-27 15:12 UTC by William Witt
Modified: 2012-10-24 23:52 UTC (History)
1 user (show)

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 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 ***