http://llvm.org/bugs/show_bug.cgi?id=17526

            Bug ID: 17526
           Summary: -Wconditional-uninitialized false positive when usage
                    is correctly constrained by other variable
           Product: new-bugs
           Version: trunk
          Hardware: Macintosh
                OS: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: new bugs
          Assignee: [email protected]
          Reporter: [email protected]
                CC: [email protected]
    Classification: Unclassified

Consider this C99:

-------------------------
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

typedef enum
{
  kDog,
  kCat,
} MyEnum;

int main (void)
{
  bool isValid = false;
  MyEnum animal;  // NB: UNINITIALIZED!

  int random = rand();
  if (random == 420)
  {
    animal = kDog;
    isValid = true;
  }

  if (isValid)
  {
    printf("animal is %u", animal);
  }

  return 0;
}
-------------------------

$ clang --version
clang version 3.4 (192323)

$ clang -Weverything -fsyntax-only ~/Desktop/test.c
/Users/sean/Desktop/test.c:25:26: warning: variable 'animal' may be
uninitialized when used here [-Wconditional-uninitialized]
                printf("animal is %u", animal);
                                       ^~~~~~
/Users/sean/Desktop/test.c:14:2: note: variable 'animal' is declared here
        MyEnum animal;  // NB: UNINITIALIZED!
        ^
1 warning generated.


It's easy for a human to see that "animal" is only used if "isValid" is true
and that everywhere "isValid" is set to true, "animal" is also set to
something.  The bug is that clang can't also see this.  :)

This is a reduced test case of something similar but with many more if-else
branches.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
LLVMbugs mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/llvmbugs

Reply via email to