blob: c5bdb86a14ed9439b61bc00d419c339bd3c39783 [file] [log] [blame]
Ted Kremenek033a07e2011-08-03 23:14:55 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=core,experimental.core.CastToStruct -analyzer-store=region -analyzer-constraints=basic -verify %s
2// RUN: %clang_cc1 -analyze -analyzer-checker=core,experimental.core.CastToStruct -analyzer-store=region -analyzer-constraints=range -verify %s
Zhongxing Xuef8b28e2008-10-17 05:19:52 +00003
Zhongxing Xu72e16822008-10-24 08:51:58 +00004struct s {
5 int data;
6 int data_array[10];
7};
Zhongxing Xuef8b28e2008-10-17 05:19:52 +00008
Zhongxing Xu234a7d22008-10-27 09:19:25 +00009typedef struct {
10 int data;
11} STYPE;
12
Zhongxing Xu91844122009-05-20 09:18:48 +000013void g(char *p);
Zhongxing Xu04b90bc2008-11-02 13:17:44 +000014void g1(struct s* p);
15
Zhongxing Xu661fc392008-11-25 01:45:11 +000016// Array to pointer conversion. Array in the struct field.
Zhongxing Xuef8b28e2008-10-17 05:19:52 +000017void f(void) {
18 int a[10];
19 int (*p)[10];
20 p = &a;
21 (*p)[3] = 1;
22
23 struct s d;
24 struct s *q;
25 q = &d;
Zhongxing Xu72e16822008-10-24 08:51:58 +000026 q->data = 3;
27 d.data_array[9] = 17;
Zhongxing Xuef8b28e2008-10-17 05:19:52 +000028}
Zhongxing Xu2e971202008-10-25 14:11:23 +000029
Zhongxing Xu661fc392008-11-25 01:45:11 +000030// StringLiteral in lvalue context and pointer to array type.
31// p: ElementRegion, q: StringRegion
Zhongxing Xu2e971202008-10-25 14:11:23 +000032void f2() {
33 char *p = "/usr/local";
34 char (*q)[4];
35 q = &"abc";
36}
Zhongxing Xu234a7d22008-10-27 09:19:25 +000037
Zhongxing Xu661fc392008-11-25 01:45:11 +000038// Typedef'ed struct definition.
Zhongxing Xu234a7d22008-10-27 09:19:25 +000039void f3() {
40 STYPE s;
41}
Zhongxing Xudf2aa1e2008-10-31 10:23:14 +000042
Zhongxing Xu661fc392008-11-25 01:45:11 +000043// Initialize array with InitExprList.
Zhongxing Xudf2aa1e2008-10-31 10:23:14 +000044void f4() {
45 int a[] = { 1, 2, 3};
46 int b[3] = { 1, 2 };
Zhongxing Xub61f49c2009-01-23 10:23:13 +000047 struct s c[] = {{1,{1}}};
Zhongxing Xudf2aa1e2008-10-31 10:23:14 +000048}
Zhongxing Xu04b90bc2008-11-02 13:17:44 +000049
Zhongxing Xu661fc392008-11-25 01:45:11 +000050// Struct variable in lvalue context.
Zhongxing Xu5834ed62009-01-13 01:49:57 +000051// Assign UnknownVal to the whole struct.
Zhongxing Xu04b90bc2008-11-02 13:17:44 +000052void f5() {
53 struct s data;
54 g1(&data);
55}
Zhongxing Xub6701332008-11-13 07:59:15 +000056
Zhongxing Xu661fc392008-11-25 01:45:11 +000057// AllocaRegion test.
Zhongxing Xub6701332008-11-13 07:59:15 +000058void f6() {
59 char *p;
60 p = __builtin_alloca(10);
Zhongxing Xu91844122009-05-20 09:18:48 +000061 g(p);
62 char c = *p;
Zhongxing Xub6701332008-11-13 07:59:15 +000063 p[1] = 'a';
Zhongxing Xu2acc3992009-05-20 09:03:10 +000064 // Test if RegionStore::EvalBinOp converts the alloca region to element
65 // region.
Zhongxing Xu262fd032009-05-20 09:00:16 +000066 p += 2;
Zhongxing Xub6701332008-11-13 07:59:15 +000067}
Zhongxing Xufb75b252008-11-13 08:44:52 +000068
69struct s2;
70
71void g2(struct s2 *p);
72
Zhongxing Xu661fc392008-11-25 01:45:11 +000073// Incomplete struct pointer used as function argument.
Zhongxing Xufb75b252008-11-13 08:44:52 +000074void f7() {
75 struct s2 *p = __builtin_alloca(10);
76 g2(p);
77}
Zhongxing Xu26134a12008-11-13 09:20:05 +000078
Zhongxing Xu661fc392008-11-25 01:45:11 +000079// sizeof() is unsigned while -1 is signed in array index.
Zhongxing Xu26134a12008-11-13 09:20:05 +000080void f8() {
81 int a[10];
Zhongxing Xu33d7cbf2008-11-24 23:45:56 +000082 a[sizeof(a)/sizeof(int) - 1] = 1; // no-warning
Zhongxing Xu26134a12008-11-13 09:20:05 +000083}
Zhongxing Xu617ff312008-11-18 13:30:46 +000084
Zhongxing Xu661fc392008-11-25 01:45:11 +000085// Initialization of struct array elements.
Zhongxing Xu617ff312008-11-18 13:30:46 +000086void f9() {
87 struct s a[10];
88}
Zhongxing Xu27cae9e2008-11-30 05:51:19 +000089
90// Initializing array with string literal.
91void f10() {
92 char a1[4] = "abc";
Zhongxing Xu27cae9e2008-11-30 05:51:19 +000093 char a3[6] = "abc";
94}
Zhongxing Xu562c4d92009-01-23 11:22:12 +000095
96// Retrieve the default value of element/field region.
97void f11() {
98 struct s a;
Zhongxing Xu91844122009-05-20 09:18:48 +000099 g1(&a);
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000100 if (a.data == 0) // no-warning
101 a.data = 1;
102}
Zhongxing Xu3450a552009-02-19 08:42:43 +0000103
104// Convert unsigned offset to signed when creating ElementRegion from
105// SymbolicRegion.
106void f12(int *list) {
107 unsigned i = 0;
108 list[i] = 1;
109}
Zhongxing Xuc57bc592009-03-18 02:07:30 +0000110
111struct s1 {
112 struct s2 {
113 int d;
114 } e;
115};
116
117// The binding of a.e.d should not be removed. Test recursive subregion map
118// building: a->e, e->d. Only then 'a' could be added to live region roots.
119void f13(double timeout) {
120 struct s1 a;
John McCall680523a2009-11-07 03:30:10 +0000121 a.e.d = (int) timeout;
Zhongxing Xuc57bc592009-03-18 02:07:30 +0000122 if (a.e.d == 10)
123 a.e.d = 4;
124}
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000125
126struct s3 {
127 int a[2];
128};
129
130static struct s3 opt;
131
132// Test if the embedded array is retrieved correctly.
133void f14() {
134 struct s3 my_opt = opt;
135}
Zhongxing Xu264e9372009-05-12 10:10:00 +0000136
137void bar(int*);
138
139// Test if the array is correctly invalidated.
140void f15() {
141 int a[10];
142 bar(a);
143 if (a[1]) // no-warning
Anders Carlsson9668b1f2009-07-30 22:37:41 +0000144 (void)1;
Zhongxing Xu264e9372009-05-12 10:10:00 +0000145}
Zhongxing Xu3f6978a2009-06-11 09:11:27 +0000146
147struct s3 p[1];
148
149// Code from postgresql.
150// Current cast logic of region store mistakenly leaves the final result region
151// an ElementRegion of type 'char'. Then load a nonloc::SymbolVal from it and
152// assigns to 'a'.
153void f16(struct s3 *p) {
Zhongxing Xu4f3dc692009-11-09 08:07:38 +0000154 struct s3 a = *((struct s3*) ((char*) &p[0])); // expected-warning{{Casting a non-structure type to a structure type and accessing a field can lead to memory access errors or data corruption.}}
Zhongxing Xu3f6978a2009-06-11 09:11:27 +0000155}
Zhongxing Xu6bd8a522009-06-28 13:59:24 +0000156
157void inv(struct s1 *);
158
159// Invalidate the struct field.
160void f17() {
161 struct s1 t;
162 int x;
163 inv(&t);
164 if (t.e.d)
165 x = 1;
166}
Zhongxing Xua03f1572009-06-29 06:43:40 +0000167
168void read(char*);
169
170void f18() {
171 char *q;
172 char *p = (char *) __builtin_alloca(10);
173 read(p);
174 q = p;
175 q++;
176 if (*q) { // no-warning
177 }
178}