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