blob: 4e888c82a275a5f5427080f77a28ee084b2d65d3 [file] [log] [blame]
Daniel Dunbard7d5f022009-03-24 02:24:46 +00001// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=basic -verify %s &&
Ted Kremenekf936f452009-05-04 06:18:28 +00002// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=range -verify %s
3
4// RegionStore now has an infinite recursion with this test case.
5// NOWORK: clang-cc -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=basic -verify %s &&
6// NOWORK: clang-cc -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=range -verify %s
Zhongxing Xuef8b28e2008-10-17 05:19:52 +00007
Zhongxing Xu72e16822008-10-24 08:51:58 +00008struct s {
9 int data;
10 int data_array[10];
11};
Zhongxing Xuef8b28e2008-10-17 05:19:52 +000012
Zhongxing Xu234a7d22008-10-27 09:19:25 +000013typedef struct {
14 int data;
15} STYPE;
16
Zhongxing Xu91844122009-05-20 09:18:48 +000017void g(char *p);
Zhongxing Xu04b90bc2008-11-02 13:17:44 +000018void g1(struct s* p);
19
Zhongxing Xu661fc392008-11-25 01:45:11 +000020// Array to pointer conversion. Array in the struct field.
Zhongxing Xuef8b28e2008-10-17 05:19:52 +000021void 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;
Zhongxing Xu72e16822008-10-24 08:51:58 +000030 q->data = 3;
31 d.data_array[9] = 17;
Zhongxing Xuef8b28e2008-10-17 05:19:52 +000032}
Zhongxing Xu2e971202008-10-25 14:11:23 +000033
Zhongxing Xu661fc392008-11-25 01:45:11 +000034// StringLiteral in lvalue context and pointer to array type.
35// p: ElementRegion, q: StringRegion
Zhongxing Xu2e971202008-10-25 14:11:23 +000036void f2() {
37 char *p = "/usr/local";
38 char (*q)[4];
39 q = &"abc";
40}
Zhongxing Xu234a7d22008-10-27 09:19:25 +000041
Zhongxing Xu661fc392008-11-25 01:45:11 +000042// Typedef'ed struct definition.
Zhongxing Xu234a7d22008-10-27 09:19:25 +000043void f3() {
44 STYPE s;
45}
Zhongxing Xudf2aa1e2008-10-31 10:23:14 +000046
Zhongxing Xu661fc392008-11-25 01:45:11 +000047// Initialize array with InitExprList.
Zhongxing Xudf2aa1e2008-10-31 10:23:14 +000048void f4() {
49 int a[] = { 1, 2, 3};
50 int b[3] = { 1, 2 };
Zhongxing Xub61f49c2009-01-23 10:23:13 +000051 struct s c[] = {{1,{1}}};
Zhongxing Xudf2aa1e2008-10-31 10:23:14 +000052}
Zhongxing Xu04b90bc2008-11-02 13:17:44 +000053
Zhongxing Xu661fc392008-11-25 01:45:11 +000054// Struct variable in lvalue context.
Zhongxing Xu5834ed62009-01-13 01:49:57 +000055// Assign UnknownVal to the whole struct.
Zhongxing Xu04b90bc2008-11-02 13:17:44 +000056void f5() {
57 struct s data;
58 g1(&data);
59}
Zhongxing Xub6701332008-11-13 07:59:15 +000060
Zhongxing Xu661fc392008-11-25 01:45:11 +000061// AllocaRegion test.
Zhongxing Xub6701332008-11-13 07:59:15 +000062void f6() {
63 char *p;
64 p = __builtin_alloca(10);
Zhongxing Xu91844122009-05-20 09:18:48 +000065 g(p);
66 char c = *p;
Zhongxing Xub6701332008-11-13 07:59:15 +000067 p[1] = 'a';
Zhongxing Xu2acc3992009-05-20 09:03:10 +000068 // Test if RegionStore::EvalBinOp converts the alloca region to element
69 // region.
Zhongxing Xu262fd032009-05-20 09:00:16 +000070 p += 2;
Zhongxing Xub6701332008-11-13 07:59:15 +000071}
Zhongxing Xufb75b252008-11-13 08:44:52 +000072
73struct s2;
74
75void g2(struct s2 *p);
76
Zhongxing Xu661fc392008-11-25 01:45:11 +000077// Incomplete struct pointer used as function argument.
Zhongxing Xufb75b252008-11-13 08:44:52 +000078void f7() {
79 struct s2 *p = __builtin_alloca(10);
80 g2(p);
81}
Zhongxing Xu26134a12008-11-13 09:20:05 +000082
Zhongxing Xu661fc392008-11-25 01:45:11 +000083// sizeof() is unsigned while -1 is signed in array index.
Zhongxing Xu26134a12008-11-13 09:20:05 +000084void f8() {
85 int a[10];
Zhongxing Xu33d7cbf2008-11-24 23:45:56 +000086 a[sizeof(a)/sizeof(int) - 1] = 1; // no-warning
Zhongxing Xu26134a12008-11-13 09:20:05 +000087}
Zhongxing Xu617ff312008-11-18 13:30:46 +000088
Zhongxing Xu661fc392008-11-25 01:45:11 +000089// Initialization of struct array elements.
Zhongxing Xu617ff312008-11-18 13:30:46 +000090void f9() {
91 struct s a[10];
92}
Zhongxing Xu27cae9e2008-11-30 05:51:19 +000093
94// Initializing array with string literal.
95void f10() {
96 char a1[4] = "abc";
Zhongxing Xu27cae9e2008-11-30 05:51:19 +000097 char a3[6] = "abc";
98}
Zhongxing Xu562c4d92009-01-23 11:22:12 +000099
100// Retrieve the default value of element/field region.
101void f11() {
102 struct s a;
Zhongxing Xu91844122009-05-20 09:18:48 +0000103 g1(&a);
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000104 if (a.data == 0) // no-warning
105 a.data = 1;
106}
Zhongxing Xu3450a552009-02-19 08:42:43 +0000107
108// Convert unsigned offset to signed when creating ElementRegion from
109// SymbolicRegion.
110void f12(int *list) {
111 unsigned i = 0;
112 list[i] = 1;
113}
Zhongxing Xuc57bc592009-03-18 02:07:30 +0000114
115struct s1 {
116 struct s2 {
117 int d;
118 } e;
119};
120
121// The binding of a.e.d should not be removed. Test recursive subregion map
122// building: a->e, e->d. Only then 'a' could be added to live region roots.
123void f13(double timeout) {
124 struct s1 a;
125 a.e.d = (long) timeout;
126 if (a.e.d == 10)
127 a.e.d = 4;
128}
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000129
130struct s3 {
131 int a[2];
132};
133
134static struct s3 opt;
135
136// Test if the embedded array is retrieved correctly.
137void f14() {
138 struct s3 my_opt = opt;
139}
Zhongxing Xu264e9372009-05-12 10:10:00 +0000140
141void bar(int*);
142
143// Test if the array is correctly invalidated.
144void f15() {
145 int a[10];
146 bar(a);
147 if (a[1]) // no-warning
148 1;
149}
Zhongxing Xu3f6978a2009-06-11 09:11:27 +0000150
151struct s3 p[1];
152
153// Code from postgresql.
154// Current cast logic of region store mistakenly leaves the final result region
155// an ElementRegion of type 'char'. Then load a nonloc::SymbolVal from it and
156// assigns to 'a'.
157void f16(struct s3 *p) {
158 struct s3 a = *((struct s3*) ((char*) &p[0]));
159}
Zhongxing Xu6bd8a522009-06-28 13:59:24 +0000160
161void inv(struct s1 *);
162
163// Invalidate the struct field.
164void f17() {
165 struct s1 t;
166 int x;
167 inv(&t);
168 if (t.e.d)
169 x = 1;
170}