Abc=(structABC*)&Ab;// expected-warning {{Casting data to a larger structure type and accessing a field can lead to memory access errors or data corruption}}
Abc=(structABC*)P;// No warning; It is not known what data P points at.
Abc=(structABC*)&*P;
// Don't warn when the cast is not widening.
P=(structAB*)&Ab;// struct AB * => struct AB *
structABCAbc2;
P=(structAB*)&Abc2;// struct ABC * => struct AB *
// True negatives when casting from Base to Derived.
DerivedD1,*D2;
Base&B1=D1;
D2=(Derived*)&B1;
D2=dynamic_cast<Derived*>(&B1);
D2=static_cast<Derived*>(&B1);
// True positives when casting from Base to Derived.
BaseB2;
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}}
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}}
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}}
// False negatives, cast from Base to Derived. With path sensitive analysis
// these false negatives could be fixed.
Base*B3=&B2;
D2=(Derived*)B3;
D2=dynamic_cast<Derived*>(B3);
D2=static_cast<Derived*>(B3);
}
voidintToStruct(int*P){
structABC*Abc;
Abc=(structABC*)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}}