1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.core.CastToStruct -verify %s
15 // Array to pointer conversion. Array in the struct field.
29 // StringLiteral in lvalue context and pointer to array type.
30 // p: ElementRegion, q: StringRegion
32 char *p
= "/usr/local";
37 // Typedef'ed struct definition.
42 // Initialize array with InitExprList.
46 struct s c
[] = {{1,{1}}};
49 // Struct variable in lvalue context.
50 // Assign UnknownVal to the whole struct.
59 p
= __builtin_alloca(10);
63 // Test if RegionStore::EvalBinOp converts the alloca region to element
70 void g2(struct s2
*p
);
72 // Incomplete struct pointer used as function argument.
74 struct s2
*p
= __builtin_alloca(10);
78 // sizeof() is unsigned while -1 is signed in array index.
81 a
[sizeof(a
)/sizeof(int) - 1] = 1; // no-warning
84 // Initialization of struct array elements.
89 // Initializing array with string literal.
95 // Retrieve the default value of element/field region.
99 if (a
.data
== 0) // no-warning
103 // Convert unsigned offset to signed when creating ElementRegion from
105 void f12(int *list
) {
116 // The binding of a.e.d should not be removed. Test recursive subregion map
117 // building: a->e, e->d. Only then 'a' could be added to live region roots.
118 void f13(double timeout
) {
120 a
.e
.d
= (int) timeout
;
129 static struct s3 opt
;
131 // Test if the embedded array is retrieved correctly.
133 struct s3 my_opt
= opt
;
138 struct s3
gets3(void) {
143 void accessArrayFieldNoCrash(void) {
149 // Test if the array is correctly invalidated.
153 if (a
[1]) // no-warning
159 // Code from postgresql.
160 // Current cast logic of region store mistakenly leaves the final result region
161 // an ElementRegion of type 'char'. Then load a nonloc::SymbolVal from it and
163 void f16(struct s3
*p
) {
164 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}}
167 void inv(struct s1
*);
169 // Invalidate the struct field.
182 char *p
= (char *) __builtin_alloca(10);
186 if (*q
) { // no-warning
191 // [PR13927] offsetof replacement macro flagged as "dereference of a null pointer"
192 int offset_of_data_array(void)
194 return ((char *)&(((struct s
*)0)->data_array
)) - ((char *)0); // no-warning
197 int testPointerArithmeticOnVoid(void *bytes
) {
199 if (&bytes
[0] == &bytes
[1])
200 return 6/p
; // no-warning
204 int testRValueArraySubscriptExpr(void *bytes
) {
205 int *p
= (int*)&bytes
[0];
207 if (*(int*)&bytes
[0] == 0)
209 return 5/(*p
); // no-warning