1 // RUN: %clang_analyze_cc1 -analyzer-checker=alpha.core.CastToStruct,core -verify %s
15 Base() : A(0), B(0) {}
22 struct Derived
: public Base
{
23 Derived() : Base(), C(0) {}
27 void structToStruct(struct AB
*P
) {
30 Abc
= (struct ABC
*)&Ab
; // expected-warning {{Casting data to a larger structure type and accessing a field can lead to memory access errors or data corruption}}
31 Abc
= (struct ABC
*)P
; // No warning; It is not known what data P points at.
32 Abc
= (struct ABC
*)&*P
;
34 // Don't warn when the cast is not widening.
35 P
= (struct AB
*)&Ab
; // struct AB * => struct AB *
37 P
= (struct AB
*)&Abc2
; // struct ABC * => struct AB *
39 // True negatives when casting from Base to Derived.
43 D2
= dynamic_cast<Derived
*>(&B1
);
44 D2
= static_cast<Derived
*>(&B1
);
46 // True positives when casting from Base to Derived.
48 D2
= (Derived
*)&B2
;// expected-warning {{Casting data to a larger structure type and accessing a field can lead to memory access errors or data corruption}}
49 D2
= dynamic_cast<Derived
*>(&B2
);// expected-warning {{Casting data to a larger structure type and accessing a field can lead to memory access errors or data corruption}}
50 D2
= static_cast<Derived
*>(&B2
);// expected-warning {{Casting data to a larger structure type and accessing a field can lead to memory access errors or data corruption}}
52 // False negatives, cast from Base to Derived. With path sensitive analysis
53 // these false negatives could be fixed.
56 D2
= dynamic_cast<Derived
*>(B3
);
57 D2
= static_cast<Derived
*>(B3
);
60 void intToStruct(int *P
) {
62 Abc
= (struct ABC
*)P
; // expected-warning {{Casting a non-structure type to a structure type and accessing a field can lead to memory access errors or data corruption}}
66 Abc
= (struct ABC
*)VP
;
69 // https://llvm.org/bugs/show_bug.cgi?id=31173
70 void dontCrash1(struct AB X
) {
71 struct UndefS
*S
= (struct UndefS
*)&X
;
78 extern struct S Var1
, Var2
;
80 ((struct T
*) &Var1
)->P
= &Var2
;