Daniel Dunbar | 4b2d0dd | 2009-02-17 22:47:27 +0000 | [diff] [blame] | 1 | // RUN: clang -analyze -checker-simple -verify %s && |
| 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> |
Daniel Dunbar | 4b2d0dd | 2009-02-17 22:47:27 +0000 | [diff] [blame] | 6 | #include "../../../../include/llvm/Config/config.h" |
Ben Laurie | 61a3778 | 2009-02-17 17:33:31 +0000 | [diff] [blame] | 7 | #ifdef HAVE_ALLOCA_H |
| 8 | # include <alloca.h> |
| 9 | #endif |
Ted Kremenek | c979a9b | 2008-11-02 00:37:31 +0000 | [diff] [blame] | 10 | |
Ted Kremenek | 02737ed | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 11 | int* f1() { |
| 12 | int x = 0; |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 13 | 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] | 14 | } |
| 15 | |
| 16 | int* f2(int y) { |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 17 | 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] | 18 | } |
| 19 | |
| 20 | int* f3(int x, int *y) { |
| 21 | int w = 0; |
| 22 | |
| 23 | if (x) |
| 24 | y = &w; |
| 25 | |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 26 | 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] | 27 | } |
| 28 | |
Ted Kremenek | fab6f22 | 2008-10-31 00:19:42 +0000 | [diff] [blame] | 29 | void* compound_literal(int x, int y) { |
Ted Kremenek | d4a0798 | 2008-10-30 18:46:50 +0000 | [diff] [blame] | 30 | if (x) |
| 31 | 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] | 32 | |
| 33 | int* array[] = {}; |
Ted Kremenek | d4a0798 | 2008-10-30 18:46:50 +0000 | [diff] [blame] | 34 | struct s { int z; double y; int w; }; |
Ted Kremenek | fab6f22 | 2008-10-31 00:19:42 +0000 | [diff] [blame] | 35 | |
| 36 | if (y) |
| 37 | return &((struct s){ 2, 0.4, 5 * 8 }); // expected-warning{{Address of stack memory}} |
| 38 | |
| 39 | |
| 40 | void* p = &((struct s){ 42, 0.4, x ? 42 : 0 }); |
Ted Kremenek | beb62c5 | 2008-10-31 00:20:13 +0000 | [diff] [blame] | 41 | return p; // expected-warning{{Address of stack memory}} |
Ted Kremenek | 194aade | 2008-10-27 21:57:17 +0000 | [diff] [blame] | 42 | } |
Ted Kremenek | 02737ed | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 43 | |
Ted Kremenek | c979a9b | 2008-11-02 00:37:31 +0000 | [diff] [blame] | 44 | void* alloca_test() { |
| 45 | void* p = alloca(10); |
| 46 | return p; // expected-warning{{Address of stack memory}} |
| 47 | } |
| 48 | |