49 lines · cpp
1// RUN: %clang_analyze_cc1 -std=c++14 -analyzer-checker=optin.osx.OSObjectCStyleCast %s -verify2#include "os_object_base.h"3 4struct OSArray : public OSObject {5 unsigned getCount();6};7 8struct A {9 int x;10};11struct B : public A {12 unsigned getCount();13};14 15unsigned warn_on_explicit_downcast(OSObject * obj) {16 OSArray *a = (OSArray *) obj; // expected-warning{{C-style cast of an OSObject is prone to type confusion attacks; use 'OSRequiredCast' if the object is definitely of type 'OSArray', or 'OSDynamicCast' followed by a null check if unsure}}17 return a->getCount();18}19 20void no_warn_on_upcast(OSArray *arr) {21 OSObject *obj = (OSObject *) arr;22 obj->retain();23 obj->release();24}25 26unsigned no_warn_on_dynamic_cast(OSObject *obj) {27 OSArray *a = OSDynamicCast(OSArray, obj);28 return a->getCount();29}30 31__SIZE_TYPE__ no_warn_on_primitive_conversion(OSArray *arr) {32 return (__SIZE_TYPE__) arr;33}34 35unsigned no_warn_on_other_type_cast(A *a) {36 B *b = (B *) a;37 return b->getCount();38}39 40unsigned no_warn_alloc_class_with_name() {41 OSArray *a = (OSArray *)OSMetaClass::allocClassWithName("OSArray"); // no warning42 return a->getCount();43}44 45unsigned warn_alloc_class_with_name() {46 OSArray *a = (OSArray *)OSMetaClass::allocClassWithName("OSObject"); // expected-warning{{C-style cast of an OSObject is prone to type confusion attacks; use 'OSRequiredCast' if the object is definitely of type 'OSArray', or 'OSDynamicCast' followed by a null check if unsure}}47 return a->getCount();48}49