array-struct.c revision 2acc3992b61e71d30653bf19be2479a78e4cd7a1
1// RUN: clang-cc -analyze -checker-simple -analyzer-store=basic -analyzer-constraints=basic -verify %s &&
2// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=basic -verify %s &&
3// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=range -verify %s
4
5// RegionStore now has an infinite recursion with this test case.
6// NOWORK: clang-cc -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=basic -verify %s &&
7// NOWORK: clang-cc -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=range -verify %s
8
9struct s {
10  int data;
11  int data_array[10];
12};
13
14typedef struct {
15  int data;
16} STYPE;
17
18void g1(struct s* p);
19
20// Array to pointer conversion. Array in the struct field.
21void f(void) {
22  int a[10];
23  int (*p)[10];
24  p = &a;
25  (*p)[3] = 1;
26
27  struct s d;
28  struct s *q;
29  q = &d;
30  q->data = 3;
31  d.data_array[9] = 17;
32}
33
34// StringLiteral in lvalue context and pointer to array type.
35// p: ElementRegion, q: StringRegion
36void f2() {
37  char *p = "/usr/local";
38  char (*q)[4];
39  q = &"abc";
40}
41
42// Typedef'ed struct definition.
43void f3() {
44  STYPE s;
45}
46
47// Initialize array with InitExprList.
48void f4() {
49  int a[] = { 1, 2, 3};
50  int b[3] = { 1, 2 };
51  struct s c[] = {{1,{1}}};
52}
53
54// Struct variable in lvalue context.
55// Assign UnknownVal to the whole struct.
56void f5() {
57  struct s data;
58  g1(&data);
59}
60
61// AllocaRegion test.
62void f6() {
63  char *p;
64  p = __builtin_alloca(10);
65  p[1] = 'a';
66  // Test if RegionStore::EvalBinOp converts the alloca region to element
67  // region.
68  p += 2;
69}
70
71struct s2;
72
73void g2(struct s2 *p);
74
75// Incomplete struct pointer used as function argument.
76void f7() {
77  struct s2 *p = __builtin_alloca(10);
78  g2(p);
79}
80
81// sizeof() is unsigned while -1 is signed in array index.
82void f8() {
83  int a[10];
84  a[sizeof(a)/sizeof(int) - 1] = 1; // no-warning
85}
86
87// Initialization of struct array elements.
88void f9() {
89  struct s a[10];
90}
91
92// Initializing array with string literal.
93void f10() {
94  char a1[4] = "abc";
95  char a3[6] = "abc";
96}
97
98// Retrieve the default value of element/field region.
99void f11() {
100  struct s a;
101  g(&a);
102  if (a.data == 0) // no-warning
103    a.data = 1;
104}
105
106// Convert unsigned offset to signed when creating ElementRegion from
107// SymbolicRegion.
108void f12(int *list) {
109  unsigned i = 0;
110  list[i] = 1;
111}
112
113struct s1 {
114  struct s2 {
115    int d;
116  } e;
117};
118
119// The binding of a.e.d should not be removed. Test recursive subregion map
120// building: a->e, e->d. Only then 'a' could be added to live region roots.
121void f13(double timeout) {
122  struct s1 a;
123  a.e.d = (long) timeout;
124  if (a.e.d == 10)
125    a.e.d = 4;
126}
127
128struct s3 {
129  int a[2];
130};
131
132static struct s3 opt;
133
134// Test if the embedded array is retrieved correctly.
135void f14() {
136  struct s3 my_opt = opt;
137}
138
139void bar(int*);
140
141// Test if the array is correctly invalidated.
142void f15() {
143  int a[10];
144  bar(a);
145  if (a[1]) // no-warning
146    1;
147}
148