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