| Argyrios Kyrtzidis | 9eb02df | 2011-02-28 19:49:42 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=core -analyzer-store=basic -fblocks -verify %s | 
|  | 2 | // RUN: %clang_cc1 -analyze -analyzer-checker=core -analyzer-store=region -fblocks -verify %s | 
| Ted Kremenek | f646774 | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 3 |  | 
|  | 4 | int* f1() { | 
|  | 5 | int x = 0; | 
| Ted Kremenek | 5df037e | 2010-06-17 04:21:37 +0000 | [diff] [blame] | 6 | return &x; // expected-warning{{Address of stack memory associated with local variable 'x' returned}} expected-warning{{address of stack memory associated with local variable 'x' returned}} | 
| Ted Kremenek | f646774 | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 7 | } | 
|  | 8 |  | 
|  | 9 | int* f2(int y) { | 
| Ted Kremenek | 5df037e | 2010-06-17 04:21:37 +0000 | [diff] [blame] | 10 | return &y;  // expected-warning{{Address of stack memory associated with local variable 'y' returned}} expected-warning{{address of stack memory associated with local variable 'y' returned}} | 
| Ted Kremenek | f646774 | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 11 | } | 
|  | 12 |  | 
|  | 13 | int* f3(int x, int *y) { | 
|  | 14 | int w = 0; | 
|  | 15 |  | 
|  | 16 | if (x) | 
|  | 17 | y = &w; | 
|  | 18 |  | 
| Ted Kremenek | 5df037e | 2010-06-17 04:21:37 +0000 | [diff] [blame] | 19 | return y; // expected-warning{{Address of stack memory associated with local variable 'w' returned to caller}} | 
| Ted Kremenek | f646774 | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 20 | } | 
|  | 21 |  | 
| Ted Kremenek | 20cf431 | 2008-10-31 00:19:42 +0000 | [diff] [blame] | 22 | void* compound_literal(int x, int y) { | 
| Ted Kremenek | b7d1380 | 2008-10-30 18:46:50 +0000 | [diff] [blame] | 23 | if (x) | 
| Eli Friedman | 0b4af8f | 2009-05-16 11:45:48 +0000 | [diff] [blame] | 24 | return &(unsigned short){((unsigned short)0x22EF)}; // expected-warning{{Address of stack memory}} | 
| Ted Kremenek | 3d71fe1 | 2008-10-30 23:17:05 +0000 | [diff] [blame] | 25 |  | 
|  | 26 | int* array[] = {}; | 
| Ted Kremenek | b7d1380 | 2008-10-30 18:46:50 +0000 | [diff] [blame] | 27 | struct s { int z; double y; int w; }; | 
| Ted Kremenek | 20cf431 | 2008-10-31 00:19:42 +0000 | [diff] [blame] | 28 |  | 
|  | 29 | if (y) | 
|  | 30 | return &((struct s){ 2, 0.4, 5 * 8 }); // expected-warning{{Address of stack memory}} | 
|  | 31 |  | 
|  | 32 |  | 
|  | 33 | void* p = &((struct s){ 42, 0.4, x ? 42 : 0 }); | 
| Ted Kremenek | d119c0b | 2008-10-31 00:20:13 +0000 | [diff] [blame] | 34 | return p; // expected-warning{{Address of stack memory}} | 
| Ted Kremenek | c173991 | 2008-10-27 21:57:17 +0000 | [diff] [blame] | 35 | } | 
| Ted Kremenek | f646774 | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 36 |  | 
| Ted Kremenek | f3d9f80 | 2008-11-02 00:37:31 +0000 | [diff] [blame] | 37 | void* alloca_test() { | 
| Eli Friedman | 61d484e | 2009-02-18 01:02:14 +0000 | [diff] [blame] | 38 | void* p = __builtin_alloca(10); | 
| Ted Kremenek | f3d9f80 | 2008-11-02 00:37:31 +0000 | [diff] [blame] | 39 | return p; // expected-warning{{Address of stack memory}} | 
|  | 40 | } | 
|  | 41 |  | 
| Ted Kremenek | a8a295f | 2009-07-01 23:24:11 +0000 | [diff] [blame] | 42 | int array_test(int x[2]) { | 
|  | 43 | return x[0]; // no-warning | 
|  | 44 | } | 
|  | 45 |  | 
| Ted Kremenek | 725b4a3 | 2009-07-02 22:02:15 +0000 | [diff] [blame] | 46 | struct baz { | 
|  | 47 | int x; | 
|  | 48 | int y[2]; | 
|  | 49 | }; | 
| Ted Kremenek | a8a295f | 2009-07-01 23:24:11 +0000 | [diff] [blame] | 50 |  | 
| Ted Kremenek | 725b4a3 | 2009-07-02 22:02:15 +0000 | [diff] [blame] | 51 | int struct_test(struct baz byVal, int flag) { | 
|  | 52 | if (flag) | 
|  | 53 | return byVal.x; // no-warning | 
|  | 54 | else { | 
|  | 55 | return byVal.y[0]; // no-warning | 
|  | 56 | } | 
| Ted Kremenek | a8a295f | 2009-07-01 23:24:11 +0000 | [diff] [blame] | 57 | } | 
| Ted Kremenek | 2350e0c | 2009-11-26 07:14:50 +0000 | [diff] [blame] | 58 |  | 
|  | 59 | typedef int (^ComparatorBlock)(int a, int b); | 
|  | 60 | ComparatorBlock test_return_block(void) { | 
|  | 61 | ComparatorBlock b = ^int(int a, int b){ return a > b; }; | 
|  | 62 | return b; // expected-warning{{Address of stack-allocated block declared on line 61 returned to caller}} | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | ComparatorBlock test_return_block_neg_aux(void); | 
|  | 66 | ComparatorBlock test_return_block_neg(void) { | 
|  | 67 | ComparatorBlock b = test_return_block_neg_aux(); | 
|  | 68 | return b; // no-warning | 
|  | 69 | } | 
|  | 70 |  | 
| Ted Kremenek | 9aa0144 | 2010-01-09 20:05:00 +0000 | [diff] [blame] | 71 | // <rdar://problem/7523821> | 
|  | 72 | int *rdar_7523821_f2() { | 
|  | 73 | int a[3]; | 
|  | 74 | return a; // expected-warning 2 {{ddress of stack memory associated with local variable 'a' returned}} | 
|  | 75 | }; | 
|  | 76 |  | 
|  | 77 |  |