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