Ted Kremenek | cdc3a89 | 2012-08-24 20:39:55 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=core,alpha.core -analyzer-store=region -analyzer-constraints=range -verify -fblocks %s |
Ted Kremenek | 3a0fd13 | 2009-04-10 04:02:38 +0000 | [diff] [blame] | 2 | |
| 3 | // <rdar://problem/6776949> |
| 4 | // main's 'argc' argument is always > 0 |
| 5 | int main(int argc, char* argv[]) { |
| 6 | int *p = 0; |
| 7 | |
| 8 | if (argc == 0) |
| 9 | *p = 1; |
| 10 | |
| 11 | if (argc == 1) |
| 12 | return 1; |
| 13 | |
| 14 | int x = 1; |
| 15 | int i; |
| 16 | |
| 17 | for(i=1;i<argc;i++){ |
| 18 | p = &x; |
| 19 | } |
| 20 | |
| 21 | return *p; // no-warning |
Daniel Dunbar | 4fcfde4 | 2009-11-08 01:45:36 +0000 | [diff] [blame] | 22 | } |
Ted Kremenek | 48569f9 | 2010-01-08 18:54:04 +0000 | [diff] [blame] | 23 | |
| 24 | // PR 5969: the comparison of argc < 3 || argc > 4 should constraint the switch |
| 25 | // statement from having the 'default' branch taken. This previously reported a false |
| 26 | // positive with the use of 'v'. |
| 27 | |
| 28 | int pr5969(int argc, char *argv[]) { |
| 29 | |
| 30 | int v; |
| 31 | |
| 32 | if ((argc < 3) || (argc > 4)) return 0; |
| 33 | |
| 34 | switch(argc) { |
| 35 | case 3: |
| 36 | v = 33; |
| 37 | break; |
| 38 | case 4: |
| 39 | v = 44; |
| 40 | break; |
| 41 | } |
| 42 | |
| 43 | return v; // no-warning |
| 44 | } |
| 45 | |
| 46 | int pr5969_positive(int argc, char *argv[]) { |
| 47 | |
| 48 | int v; |
| 49 | |
| 50 | if ((argc < 3) || (argc > 4)) return 0; |
| 51 | |
| 52 | switch(argc) { |
| 53 | case 3: |
| 54 | v = 33; |
| 55 | break; |
| 56 | } |
| 57 | |
| 58 | return v; // expected-warning{{Undefined or garbage value returned to caller}} |
| 59 | } |