blob: 5d1ce253fc35f94067e617446936752bb95a4273 [file] [log] [blame]
Ted Kremeneka1c57162009-11-26 07:14:50 +00001// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -fblocks -verify %s
2// RUN: clang-cc -analyze -checker-cfref -analyzer-store=region -fblocks -verify %s
Ted Kremenek02737ed2008-03-31 15:02:58 +00003
4int* f1() {
5 int x = 0;
Ted Kremenek22bda882008-07-31 20:31:27 +00006 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 Kremenek02737ed2008-03-31 15:02:58 +00007}
8
9int* f2(int y) {
Ted Kremenek22bda882008-07-31 20:31:27 +000010 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 Kremenek02737ed2008-03-31 15:02:58 +000011}
12
13int* f3(int x, int *y) {
14 int w = 0;
15
16 if (x)
17 y = &w;
18
Ted Kremenek22bda882008-07-31 20:31:27 +000019 return y; // expected-warning{{Address of stack memory associated with local variable 'w' returned.}}
Ted Kremenek02737ed2008-03-31 15:02:58 +000020}
21
Ted Kremenekfab6f222008-10-31 00:19:42 +000022void* compound_literal(int x, int y) {
Ted Kremenekd4a07982008-10-30 18:46:50 +000023 if (x)
Eli Friedman759f2522009-05-16 11:45:48 +000024 return &(unsigned short){((unsigned short)0x22EF)}; // expected-warning{{Address of stack memory}}
Ted Kremenek64cc62d2008-10-30 23:17:05 +000025
26 int* array[] = {};
Ted Kremenekd4a07982008-10-30 18:46:50 +000027 struct s { int z; double y; int w; };
Ted Kremenekfab6f222008-10-31 00:19:42 +000028
29 if (y)
30 return &((struct s){ 2, 0.4, 5 * 8 }); // expected-warning{{Address of stack memory}}
31
32
33 void* p = &((struct s){ 42, 0.4, x ? 42 : 0 });
Ted Kremenekbeb62c52008-10-31 00:20:13 +000034 return p; // expected-warning{{Address of stack memory}}
Ted Kremenek194aade2008-10-27 21:57:17 +000035}
Ted Kremenek02737ed2008-03-31 15:02:58 +000036
Ted Kremenekc979a9b2008-11-02 00:37:31 +000037void* alloca_test() {
Eli Friedmane1d77c32009-02-18 01:02:14 +000038 void* p = __builtin_alloca(10);
Ted Kremenekc979a9b2008-11-02 00:37:31 +000039 return p; // expected-warning{{Address of stack memory}}
40}
41
Ted Kremenekdac5bd42009-07-01 23:24:11 +000042int array_test(int x[2]) {
43 return x[0]; // no-warning
44}
45
Ted Kremenekdc147262009-07-02 22:02:15 +000046struct baz {
47 int x;
48 int y[2];
49};
Ted Kremenekdac5bd42009-07-01 23:24:11 +000050
Ted Kremenekdc147262009-07-02 22:02:15 +000051int struct_test(struct baz byVal, int flag) {
52 if (flag)
53 return byVal.x; // no-warning
54 else {
55 return byVal.y[0]; // no-warning
56 }
Ted Kremenekdac5bd42009-07-01 23:24:11 +000057}
Ted Kremeneka1c57162009-11-26 07:14:50 +000058
59typedef int (^ComparatorBlock)(int a, int b);
60ComparatorBlock test_return_block(void) {
61 ComparatorBlock b = ^int(int a, int b){ return a > b; };
62 return b; // expected-warning{{Address of stack-allocated block declared on line 61 returned to caller}}
63}
64
65ComparatorBlock test_return_block_neg_aux(void);
66ComparatorBlock test_return_block_neg(void) {
67 ComparatorBlock b = test_return_block_neg_aux();
68 return b; // no-warning
69}
70