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