127 lines · cpp
1// RUN: %clang_analyze_cc1 -analyzer-checker=core -verify -std=c++2a %s2 3 4static const unsigned long long scull = 0;5void static_int()6{7 *(int*)scull = 0; // expected-warning{{Dereference of null pointer}}8}9 10const unsigned long long cull = 0;11void const_int()12{13 *(int*)cull = 0; // expected-warning{{Dereference of null pointer}}14}15 16static int * const spc = 0;17void static_ptr()18{19 *spc = 0; // expected-warning{{Dereference of null pointer}}20}21 22int * const pc = 0;23void const_ptr()24{25 *pc = 0; // expected-warning{{Dereference of null pointer}}26}27 28const unsigned long long cull_nonnull = 4;29void nonnull_int()30{31 *(int*)(cull_nonnull - 4) = 0; // expected-warning{{Dereference of null pointer}}32}33 34int * const pc_nonnull = (int*)sizeof(int);35void nonnull_ptr()36{37 *(pc_nonnull - 1) = 0; // expected-warning{{Dereference of null pointer}}38}39 40int * const constcast = const_cast<int * const>((int*)sizeof(int));41void cast1()42{43 *(constcast - 1) = 0; // expected-warning{{Dereference of null pointer}}44}45 46int * const recast = reinterpret_cast<int*>(sizeof(int));47void cast2()48{49 *(recast - 1) = 0; // expected-warning{{Dereference of null pointer}}50}51 52int * const staticcast = static_cast<int * const>((int*)sizeof(int));53void cast3()54{55 *(staticcast - 1) = 0; // expected-warning{{Dereference of null pointer}}56}57 58struct Foo { int a; };59Foo * const dyncast = dynamic_cast<Foo * const>((Foo*)sizeof(Foo));60void cast4()61{62 // Do not handle dynamic_cast for now, because it may change the pointer value.63 (dyncast - 1)->a = 0; // no-warning64}65 66typedef int * const intptrconst;67int * const funccast = intptrconst(sizeof(int));68void cast5()69{70 *(funccast - 1) = 0; // expected-warning{{Dereference of null pointer}}71}72 73struct S174{75 int * p;76};77const S1 s1 = {78 .p = (int*)sizeof(int)79};80void conststruct()81{82 *(s1.p - 1) = 0; // expected-warning{{Dereference of null pointer}}83}84 85struct S286{87 int * const p;88};89S2 s2 = {90 .p = (int*)sizeof(int)91};92void constfield()93{94 *(s2.p - 1) = 0; // expected-warning{{Dereference of null pointer}}95}96 97int * const parr[1] = { (int*)sizeof(int) };98void constarr()99{100 *(parr[0] - 1) = 0; // expected-warning{{Dereference of null pointer}}101}102 103struct S3104{105 int * p = (int*)sizeof(int);106};107void recordinit()108{109 S3 s3;110 *(s3.p - 1) = 0; // expected-warning{{Dereference of null pointer}}111}112 113extern int ext_int;114 115void update_original_declaration() {116 ext_int = 2;117}118 119extern int ext_int;120 121int test_redeclaration() {122 ext_int = 1;123 update_original_declaration();124 int int_int = 3 / (ext_int - 1); // no-warning125 return int_int / (ext_int - 2); // expected-warning{{Division by zero}}126}127