Ted Kremenek | a1c5716 | 2009-11-26 07:14:50 +0000 | [diff] [blame] | 1 | // RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -fblocks -verify %s |
| 2 | // RUN: clang-cc -analyze -checker-cfref -analyzer-store=region -fblocks -verify %s |
Ted Kremenek | 02737ed | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 3 | |
| 4 | int* f1() { |
| 5 | int x = 0; |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +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 | 02737ed | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 7 | } |
| 8 | |
| 9 | int* f2(int y) { |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +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 | 02737ed | 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 | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 19 | return y; // expected-warning{{Address of stack memory associated with local variable 'w' returned.}} |
Ted Kremenek | 02737ed | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 20 | } |
| 21 | |
Ted Kremenek | fab6f22 | 2008-10-31 00:19:42 +0000 | [diff] [blame] | 22 | void* compound_literal(int x, int y) { |
Ted Kremenek | d4a0798 | 2008-10-30 18:46:50 +0000 | [diff] [blame] | 23 | if (x) |
Eli Friedman | 759f252 | 2009-05-16 11:45:48 +0000 | [diff] [blame] | 24 | return &(unsigned short){((unsigned short)0x22EF)}; // expected-warning{{Address of stack memory}} |
Ted Kremenek | 64cc62d | 2008-10-30 23:17:05 +0000 | [diff] [blame] | 25 | |
| 26 | int* array[] = {}; |
Ted Kremenek | d4a0798 | 2008-10-30 18:46:50 +0000 | [diff] [blame] | 27 | struct s { int z; double y; int w; }; |
Ted Kremenek | fab6f22 | 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 | beb62c5 | 2008-10-31 00:20:13 +0000 | [diff] [blame] | 34 | return p; // expected-warning{{Address of stack memory}} |
Ted Kremenek | 194aade | 2008-10-27 21:57:17 +0000 | [diff] [blame] | 35 | } |
Ted Kremenek | 02737ed | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 36 | |
Ted Kremenek | c979a9b | 2008-11-02 00:37:31 +0000 | [diff] [blame] | 37 | void* alloca_test() { |
Eli Friedman | e1d77c3 | 2009-02-18 01:02:14 +0000 | [diff] [blame] | 38 | void* p = __builtin_alloca(10); |
Ted Kremenek | c979a9b | 2008-11-02 00:37:31 +0000 | [diff] [blame] | 39 | return p; // expected-warning{{Address of stack memory}} |
| 40 | } |
| 41 | |
Ted Kremenek | dac5bd4 | 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 | dc14726 | 2009-07-02 22:02:15 +0000 | [diff] [blame] | 46 | struct baz { |
| 47 | int x; |
| 48 | int y[2]; |
| 49 | }; |
Ted Kremenek | dac5bd4 | 2009-07-01 23:24:11 +0000 | [diff] [blame] | 50 | |
Ted Kremenek | dc14726 | 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 | dac5bd4 | 2009-07-01 23:24:11 +0000 | [diff] [blame] | 57 | } |
Ted Kremenek | a1c5716 | 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 | |