blob: 3516fee0a4e9db3f6c106a99ebd215c2e1bb5172 [file] [log] [blame]
Daniel Dunbar4b2d0dd2009-02-17 22:47:27 +00001// 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 Kremenek02737ed2008-03-31 15:02:58 +00004
Ted Kremenekc979a9b2008-11-02 00:37:31 +00005#include <stdlib.h>
Daniel Dunbar4b2d0dd2009-02-17 22:47:27 +00006#include "../../../../include/llvm/Config/config.h"
Ben Laurie61a37782009-02-17 17:33:31 +00007#ifdef HAVE_ALLOCA_H
8# include <alloca.h>
9#endif
Ted Kremenekc979a9b2008-11-02 00:37:31 +000010
Ted Kremenek02737ed2008-03-31 15:02:58 +000011int* f1() {
12 int x = 0;
Ted Kremenek22bda882008-07-31 20:31:27 +000013 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 +000014}
15
16int* f2(int y) {
Ted Kremenek22bda882008-07-31 20:31:27 +000017 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 +000018}
19
20int* f3(int x, int *y) {
21 int w = 0;
22
23 if (x)
24 y = &w;
25
Ted Kremenek22bda882008-07-31 20:31:27 +000026 return y; // expected-warning{{Address of stack memory associated with local variable 'w' returned.}}
Ted Kremenek02737ed2008-03-31 15:02:58 +000027}
28
Ted Kremenekfab6f222008-10-31 00:19:42 +000029void* compound_literal(int x, int y) {
Ted Kremenekd4a07982008-10-30 18:46:50 +000030 if (x)
31 return &(unsigned short){((unsigned short)0x22EF)}; // expected-warning{{Address of stack memory}} expected-warning{{braces around scalar initializer}}
Ted Kremenek64cc62d2008-10-30 23:17:05 +000032
33 int* array[] = {};
Ted Kremenekd4a07982008-10-30 18:46:50 +000034 struct s { int z; double y; int w; };
Ted Kremenekfab6f222008-10-31 00:19:42 +000035
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 Kremenekbeb62c52008-10-31 00:20:13 +000041 return p; // expected-warning{{Address of stack memory}}
Ted Kremenek194aade2008-10-27 21:57:17 +000042}
Ted Kremenek02737ed2008-03-31 15:02:58 +000043
Ted Kremenekc979a9b2008-11-02 00:37:31 +000044void* alloca_test() {
45 void* p = alloca(10);
46 return p; // expected-warning{{Address of stack memory}}
47}
48