Ted Kremenek | dc14726 | 2009-07-02 22:02:15 +0000 | [diff] [blame^] | 1 | // RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -verify %s && |
| 2 | // RUN: clang-cc -analyze -checker-cfref -analyzer-store=region -verify %s |
Ted Kremenek | 02737ed | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 3 | |
Ted Kremenek | c979a9b | 2008-11-02 00:37:31 +0000 | [diff] [blame] | 4 | #include <stdlib.h> |
| 5 | |
Ted Kremenek | 02737ed | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 6 | int* f1() { |
| 7 | int x = 0; |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 8 | 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] | 9 | } |
| 10 | |
| 11 | int* f2(int y) { |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 12 | 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] | 13 | } |
| 14 | |
| 15 | int* f3(int x, int *y) { |
| 16 | int w = 0; |
| 17 | |
| 18 | if (x) |
| 19 | y = &w; |
| 20 | |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 21 | 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] | 22 | } |
| 23 | |
Ted Kremenek | fab6f22 | 2008-10-31 00:19:42 +0000 | [diff] [blame] | 24 | void* compound_literal(int x, int y) { |
Ted Kremenek | d4a0798 | 2008-10-30 18:46:50 +0000 | [diff] [blame] | 25 | if (x) |
Eli Friedman | 759f252 | 2009-05-16 11:45:48 +0000 | [diff] [blame] | 26 | return &(unsigned short){((unsigned short)0x22EF)}; // expected-warning{{Address of stack memory}} |
Ted Kremenek | 64cc62d | 2008-10-30 23:17:05 +0000 | [diff] [blame] | 27 | |
| 28 | int* array[] = {}; |
Ted Kremenek | d4a0798 | 2008-10-30 18:46:50 +0000 | [diff] [blame] | 29 | struct s { int z; double y; int w; }; |
Ted Kremenek | fab6f22 | 2008-10-31 00:19:42 +0000 | [diff] [blame] | 30 | |
| 31 | if (y) |
| 32 | return &((struct s){ 2, 0.4, 5 * 8 }); // expected-warning{{Address of stack memory}} |
| 33 | |
| 34 | |
| 35 | void* p = &((struct s){ 42, 0.4, x ? 42 : 0 }); |
Ted Kremenek | beb62c5 | 2008-10-31 00:20:13 +0000 | [diff] [blame] | 36 | return p; // expected-warning{{Address of stack memory}} |
Ted Kremenek | 194aade | 2008-10-27 21:57:17 +0000 | [diff] [blame] | 37 | } |
Ted Kremenek | 02737ed | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 38 | |
Ted Kremenek | c979a9b | 2008-11-02 00:37:31 +0000 | [diff] [blame] | 39 | void* alloca_test() { |
Eli Friedman | e1d77c3 | 2009-02-18 01:02:14 +0000 | [diff] [blame] | 40 | void* p = __builtin_alloca(10); |
Ted Kremenek | c979a9b | 2008-11-02 00:37:31 +0000 | [diff] [blame] | 41 | return p; // expected-warning{{Address of stack memory}} |
| 42 | } |
| 43 | |
Ted Kremenek | dac5bd4 | 2009-07-01 23:24:11 +0000 | [diff] [blame] | 44 | int array_test(int x[2]) { |
| 45 | return x[0]; // no-warning |
| 46 | } |
| 47 | |
Ted Kremenek | dc14726 | 2009-07-02 22:02:15 +0000 | [diff] [blame^] | 48 | struct baz { |
| 49 | int x; |
| 50 | int y[2]; |
| 51 | }; |
Ted Kremenek | dac5bd4 | 2009-07-01 23:24:11 +0000 | [diff] [blame] | 52 | |
Ted Kremenek | dc14726 | 2009-07-02 22:02:15 +0000 | [diff] [blame^] | 53 | int struct_test(struct baz byVal, int flag) { |
| 54 | if (flag) |
| 55 | return byVal.x; // no-warning |
| 56 | else { |
| 57 | return byVal.y[0]; // no-warning |
| 58 | } |
Ted Kremenek | dac5bd4 | 2009-07-01 23:24:11 +0000 | [diff] [blame] | 59 | } |