blob: c604753c6e2a1608cc61d896fd4087038c319898 [file] [log] [blame]
Ted Kremenekb1983ba2008-04-10 22:16:52 +00001// RUN: clang -checker-simple -verify %s
Ted Kremenek108048c2008-03-31 15:02:58 +00002
Ted Kremenek1be98f82008-11-02 00:37:31 +00003#include <stdlib.h>
Argiris Kirtzidisda5aa1d2008-11-07 14:00:25 +00004#include <malloc.h>
Ted Kremenek1be98f82008-11-02 00:37:31 +00005
Ted Kremenek108048c2008-03-31 15:02:58 +00006int* f1() {
7 int x = 0;
Ted Kremenek0303bab2008-07-31 20:31:27 +00008 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 Kremenek108048c2008-03-31 15:02:58 +00009}
10
11int* f2(int y) {
Ted Kremenek0303bab2008-07-31 20:31:27 +000012 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 Kremenek108048c2008-03-31 15:02:58 +000013}
14
15int* f3(int x, int *y) {
16 int w = 0;
17
18 if (x)
19 y = &w;
20
Ted Kremenek0303bab2008-07-31 20:31:27 +000021 return y; // expected-warning{{Address of stack memory associated with local variable 'w' returned.}}
Ted Kremenek108048c2008-03-31 15:02:58 +000022}
23
Ted Kremenekea4a4fc2008-10-31 00:19:42 +000024void* compound_literal(int x, int y) {
Ted Kremenek3ffc99c2008-10-30 18:46:50 +000025 if (x)
26 return &(unsigned short){((unsigned short)0x22EF)}; // expected-warning{{Address of stack memory}} expected-warning{{braces around scalar initializer}}
Ted Kremenek069b9582008-10-30 23:17:05 +000027
28 int* array[] = {};
Ted Kremenek3ffc99c2008-10-30 18:46:50 +000029 struct s { int z; double y; int w; };
Ted Kremenekea4a4fc2008-10-31 00:19:42 +000030
31 if (y)
32 return &((struct s){ 2, 0.4, 5 * 8 }); // expected-warning{{Address of stack memory}}
33
34
35 void* p = &((struct s){ 42, 0.4, x ? 42 : 0 });
Ted Kremenekc4cf07c2008-10-31 00:20:13 +000036 return p; // expected-warning{{Address of stack memory}}
Ted Kremenek1a237f22008-10-27 21:57:17 +000037}
Ted Kremenek108048c2008-03-31 15:02:58 +000038
Ted Kremenek1be98f82008-11-02 00:37:31 +000039void* alloca_test() {
40 void* p = alloca(10);
41 return p; // expected-warning{{Address of stack memory}}
42}
43