stack-addr-ps.c revision be1fe1eb12a1cb91c8e3a9fcc2db4dfe989def6c
1// 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
4
5#include <stdlib.h>
6#include <alloca.h>
7
8int* f1() {
9  int x = 0;
10  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}}
11}
12
13int* f2(int y) {
14  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}}
15}
16
17int* f3(int x, int *y) {
18  int w = 0;
19
20  if (x)
21    y = &w;
22
23  return y; // expected-warning{{Address of stack memory associated with local variable 'w' returned.}}
24}
25
26void* compound_literal(int x, int y) {
27  if (x)
28    return &(unsigned short){((unsigned short)0x22EF)}; // expected-warning{{Address of stack memory}} expected-warning{{braces around scalar initializer}}
29
30  int* array[] = {};
31  struct s { int z; double y; int w; };
32
33  if (y)
34    return &((struct s){ 2, 0.4, 5 * 8 }); // expected-warning{{Address of stack memory}}
35
36
37  void* p = &((struct s){ 42, 0.4, x ? 42 : 0 });
38  return p; // expected-warning{{Address of stack memory}}
39}
40
41void* alloca_test() {
42  void* p = alloca(10);
43  return p; // expected-warning{{Address of stack memory}}
44}
45
46