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