stack-addr-ps.c revision 61a3778c81c8d3e91f19479b300336f23ac991d4
1// RUN: clang -analyze -checker-simple -verify -I../../../include %s &&
2// RUN: clang -analyze -checker-cfref -analyzer-store=basic -verify -I../../../include %s &&
3// RUN: clang -analyze -checker-cfref -analyzer-store=region -verify -I../../../include %s
4
5#include <stdlib.h>
6#include <llvm/Config/config.h>
7#ifdef HAVE_ALLOCA_H
8# include <alloca.h>
9#endif
10
11int* f1() {
12  int x = 0;
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}}
14}
15
16int* f2(int y) {
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}}
18}
19
20int* f3(int x, int *y) {
21  int w = 0;
22
23  if (x)
24    y = &w;
25
26  return y; // expected-warning{{Address of stack memory associated with local variable 'w' returned.}}
27}
28
29void* compound_literal(int x, int y) {
30  if (x)
31    return &(unsigned short){((unsigned short)0x22EF)}; // expected-warning{{Address of stack memory}} expected-warning{{braces around scalar initializer}}
32
33  int* array[] = {};
34  struct s { int z; double y; int w; };
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 });
41  return p; // expected-warning{{Address of stack memory}}
42}
43
44void* alloca_test() {
45  void* p = alloca(10);
46  return p; // expected-warning{{Address of stack memory}}
47}
48
49