Debian Codegraph @

clang/test/Analysis/casts.c

raw
1 // RUN: %clang_analyze_cc1 -triple x86_64-apple-darwin9 -fenable-matrix -analyzer-checker=core,alpha.core,debug.ExprInspection -Wno-pointer-to-int-cast -Wno-strict-prototypes -verify -analyzer-config eagerly-assume=false %s
2 // RUN: %clang_analyze_cc1 -triple i386-apple-darwin9 -fenable-matrix -analyzer-checker=core,alpha.core,debug.ExprInspection -Wno-pointer-to-int-cast -Wno-strict-prototypes -verify -analyzer-config eagerly-assume=false %s
3 // RUN: %clang_analyze_cc1 -triple x86_64-apple-darwin9 -fenable-matrix -analyzer-checker=core,alpha.core,debug.ExprInspection -Wno-pointer-to-int-cast -Wno-strict-prototypes -verify -DEAGERLY_ASSUME=1 -w %s
4 // RUN: %clang_analyze_cc1 -triple i386-apple-darwin9 -fenable-matrix -analyzer-checker=core,alpha.core,debug.ExprInspection -Wno-pointer-to-int-cast -Wno-strict-prototypes -verify -DEAGERLY_ASSUME=1 -DBIT32=1 -w %s
5
6 extern void clang_analyzer_eval(_Bool);
7
8 // Test if the 'storage' region gets properly initialized after it is cast to
9 // 'struct sockaddr *'.
10
11 typedef unsigned char __uint8_t;
12 typedef unsigned int __uint32_t;
13 typedef __uint32_t __darwin_socklen_t;
14 typedef __uint8_t sa_family_t;
15 typedef __darwin_socklen_t socklen_t;
16 struct sockaddr { sa_family_t sa_family; };
17 struct sockaddr_storage {};
18
19 void getsockname();
20
21 #ifndef EAGERLY_ASSUME
22
23 void f(int sock) {
24 struct sockaddr_storage storage;
25 struct sockaddr* sockaddr = (struct sockaddr*)&storage; // expected-warning{{Casting data to a larger structure type and accessing a field can lead to memory access errors or data corruption}}
26 socklen_t addrlen = sizeof(storage);
27 getsockname(sock, sockaddr, &addrlen);
28 switch (sockaddr->sa_family) { // no-warning
29 default:
30 ;
31 }
32 }
33
34 struct s {
35 struct s *value;
36 };
37
38 void f1(struct s **pval) {
39 int *tbool = ((void*)0);
40 struct s *t = *pval;
41 pval = &(t->value);
42 tbool = (int *)pval; // use the cast-to type 'int *' to create element region.
43 char c = (unsigned char) *tbool; // Should use cast-to type to create symbol.
44 if (*tbool == -1) // here load the element region with the correct type 'int'
45 (void)3;
46 }
47
48 void f2(const char *str) {
49 unsigned char ch, cl, *p;
50
51 p = (unsigned char *)str;
52 ch = *p++; // use cast-to type 'unsigned char' to create element region.
53 cl = *p++;
54 if(!cl)
55 cl = 'a';
56 }
57
58 // Test cast VariableSizeArray to pointer does not crash.
59 void *memcpy(void *, void const *, unsigned long);
60 typedef unsigned char Byte;
61 void doit(char *data, int len) {
62 if (len) {
63 Byte buf[len];
64 memcpy(buf, data, len);
65 }
66 }
67
68 // PR 6013 and 6035 - Test that a cast of a pointer to long and then to int does not crash SValuator.
69 void pr6013_6035_test(void *p) {
70 unsigned int foo;
71 foo = ((long)(p));
72 (void) foo;
73 }
74
75 // PR12511 - Test that we support SymCastExpr, which represents symbolic int to float cast.
76 char ttt(int intSeconds) {
77 double seconds = intSeconds;
78 if (seconds)
79 return 0;
80 return 0;
81 }
82
83 int foo (int* p) {
84 int y = 0;
85 if (p == 0) {
86 if ((*((void**)&p)) == (void*)0) // Test that the cast to void preserves the symbolic region.
87 return 0;
88 else
89 return 5/y; // This code should be unreachable: no-warning.
90 }
91 return 0;
92 }
93
94 void castsToBool(void) {
95 clang_analyzer_eval(0); // expected-warning{{FALSE}}
96 clang_analyzer_eval(0U); // expected-warning{{FALSE}}
97 clang_analyzer_eval((void *)0); // expected-warning{{FALSE}}
98
99 clang_analyzer_eval(1); // expected-warning{{TRUE}}
100 clang_analyzer_eval(1U); // expected-warning{{TRUE}}
101 clang_analyzer_eval(-1); // expected-warning{{TRUE}}
102 clang_analyzer_eval(0x100); // expected-warning{{TRUE}}
103 clang_analyzer_eval(0x100U); // expected-warning{{TRUE}}
104 clang_analyzer_eval((void *)0x100); // expected-warning{{TRUE}}
105
106 extern int symbolicInt;
107 clang_analyzer_eval(symbolicInt); // expected-warning{{UNKNOWN}}
108 if (symbolicInt)
109 clang_analyzer_eval(symbolicInt); // expected-warning{{TRUE}}
110
111 extern void *symbolicPointer;
112 clang_analyzer_eval(symbolicPointer); // expected-warning{{UNKNOWN}}
113 if (symbolicPointer)
114 clang_analyzer_eval(symbolicPointer); // expected-warning{{TRUE}}
115
116 int localInt;
117 int* ptr = &localInt;
118 clang_analyzer_eval(ptr); // expected-warning{{TRUE}}
119 clang_analyzer_eval(&castsToBool); // expected-warning{{TRUE}}
120 clang_analyzer_eval("abc"); // expected-warning{{TRUE}}
121
122 extern float globalFloat;
123 clang_analyzer_eval(globalFloat); // expected-warning{{UNKNOWN}}
124 }
125
126 void locAsIntegerCasts(void *p) {
127 int x = (int) p;
128 clang_analyzer_eval(++x < 10); // no-crash // expected-warning{{UNKNOWN}}
129 }
130
131 void multiDimensionalArrayPointerCasts(void) {
132 static int x[10][10]; // expected-note2{{Array at the right-hand side of subtraction}}
133 int *y1 = &(x[3][5]);
134 char *z = ((char *) y1) + 2;
135 int *y2 = (int *)(z - 2);
136 int *y3 = ((int *)x) + 35; // This is offset for [3][5].
137
138 clang_analyzer_eval(y1 == y2); // expected-warning{{TRUE}}
139
140 // FIXME: should be FALSE (i.e. equal pointers).
141 // FIXME: pointer subtraction warning might be incorrect
142 clang_analyzer_eval(y1 - y2); // expected-warning{{UNKNOWN}}
143 // expected-warning@-1{{Subtraction of two pointers that do not point into the same array is undefined behavior}}
144 // FIXME: should be TRUE (i.e. same symbol).
145 clang_analyzer_eval(*y1 == *y2); // expected-warning{{UNKNOWN}}
146
147 clang_analyzer_eval(*((char *)y1) == *((char *) y2)); // expected-warning{{TRUE}}
148
149 clang_analyzer_eval(y1 == y3); // expected-warning{{TRUE}}
150
151 // FIXME: should be FALSE (i.e. equal pointers).
152 // FIXME: pointer subtraction warning might be incorrect
153 clang_analyzer_eval(y1 - y3); // expected-warning{{UNKNOWN}}
154 // expected-warning@-1{{Subtraction of two pointers that do not point into the same array is undefined behavior}}
155 // FIXME: should be TRUE (i.e. same symbol).
156 clang_analyzer_eval(*y1 == *y3); // expected-warning{{UNKNOWN}}
157
158 clang_analyzer_eval(*((char *)y1) == *((char *) y3)); // expected-warning{{TRUE}}
159 }
160
161 void *getVoidPtr(void);
162
163 void testCastVoidPtrToIntPtrThroughIntTypedAssignment(void) {
164 int *x;
165 (*((int *)(&x))) = (int)getVoidPtr();
166 *x = 1; // no-crash
167 }
168
169 void testCastUIntPtrToIntPtrThroughIntTypedAssignment(void) {
170 unsigned u;
171 int *x;
172 (*((int *)(&x))) = (int)&u;
173 *x = 1;
174 clang_analyzer_eval(u == 1); // expected-warning{{TRUE}}
175 }
176
177 void testCastVoidPtrToIntPtrThroughUIntTypedAssignment(void) {
178 int *x;
179 (*((int *)(&x))) = (int)(unsigned *)getVoidPtr();
180 *x = 1; // no-crash
181 }
182
183 void testLocNonLocSymbolAssume(int a, int *b) {
184 if ((int)b < a) {} // no-crash
185 }
186
187 void testLocNonLocSymbolRemainder(int a, int *b) {
188 int c = ((int)b) % a;
189 if (a == 1) {
190 c += 1;
191 }
192 }
193
194 void testSwitchWithSizeofs(void) {
195 switch (sizeof(char) == 1) { // expected-warning{{switch condition has boolean value}}
196 case sizeof(char):; // no-crash
197 }
198 }
199
200 void test_ToUnion_cast(unsigned long long x) {
201 union Key {
202 unsigned long long data;
203 };
204 void clang_analyzer_dump_union(union Key);
205 clang_analyzer_dump_union((union Key)x); // expected-warning {{Unknown}}
206 }
207
208 typedef char cx5x5 __attribute__((matrix_type(5, 5)));
209 typedef int ix5x5 __attribute__((matrix_type(5, 5)));
210 void test_MatrixCast_cast(cx5x5 c) {
211 void clang_analyzer_dump_ix5x5(ix5x5);
212 clang_analyzer_dump_ix5x5((ix5x5)c); // expected-warning {{Unknown}}
213 }
214
215 void test_VectorSplat_cast(long x) {
216 typedef int __attribute__((ext_vector_type(2))) V;
217 void clang_analyzer_dump_V(V);
218 clang_analyzer_dump_V((V)x); // expected-warning {{Unknown}}
219 }
220
221 #endif
222
223 #ifdef EAGERLY_ASSUME
224
225 int globalA;
226 extern int globalFunc(void);
227 void no_crash_on_symsym_cast_to_long(void) {
228 char c = globalFunc() - 5;
229 c == 0;
230 globalA -= c;
231 globalA == 3;
232 (long)globalA << 48;
233 #ifdef BIT32
234 // expected-warning@-2{{Left shift by '48' overflows the capacity of 'long'}}
235 #else
236 // expected-no-diagnostics
237 #endif
238 }
239
240 #endif
241
242 char no_crash_SymbolCast_of_float_type_aux(int *p) {
243 *p += 1;
244 return *p;
245 }
246
247 void no_crash_SymbolCast_of_float_type(void) {
248 extern float x;
249 char (*f)() = no_crash_SymbolCast_of_float_type_aux;
250 f(&x);
251 }
252
253 double no_crash_reinterpret_double_as_int(double a) {
254 *(int *)&a = 1;
255 return a * a;
256 }
257
258 double no_crash_reinterpret_double_as_ptr(double a) {
259 *(void **)&a = 0;
260 return a * a;
261 }
262
263 double no_crash_reinterpret_double_as_sym_int(double a, int b) {
264 *(int *)&a = b;
265 return a * a;
266 }
267
268 double no_crash_reinterpret_double_as_sym_ptr(double a, void * b) {
269 *(void **)&a = b;
270 return a * a;
271 }
272
273 void no_crash_reinterpret_char_as_uchar(char ***a, int *b) {
274 *(unsigned char **)a = (unsigned char *)b;
275 if (**a == 0) // no-crash
276 ;
277 }
278
279 // PR50179.
280 struct S {};
281 void symbolic_offset(struct S *ptr, int i) {
282 const struct S *pS = ptr + i;
283 struct S s = *pS; // no-crash
284 }