false-positive-suppression.cpp
3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// RUN: %clang_analyze_cc1 -analyzer-checker=core -analyzer-config suppress-null-return-paths=false -verify %s
// RUN: %clang_analyze_cc1 -analyzer-checker=core -verify -DSUPPRESSED=1 %s
namespace rdar12676053 {
// Delta-reduced from a preprocessed file.
template<class T>
class RefCount {
T *ref;
public:
T *operator->() const {
return ref ? ref : 0;
}
};
class string {};
class ParserInputState {
public:
string filename;
};
class Parser {
void setFilename(const string& f) {
inputState->filename = f;
#ifndef SUPPRESSED
// expected-warning@-2 {{Called C++ object pointer is null}}
#endif
}
protected:
RefCount<ParserInputState> inputState;
};
}
// This is the standard placement new.
inline void* operator new(__typeof__(sizeof(int)), void* __p) throw()
{
return __p;
}
extern bool coin();
class SomeClass {
public:
void doSomething();
};
namespace References {
class Map {
int *&getNewBox();
int *firstBox;
public:
int *&getValue(int key) {
if (coin()) {
return firstBox;
} else {
int *&newBox = getNewBox();
newBox = 0;
return newBox;
}
}
int *&getValueIndirectly(int key) {
int *&valueBox = getValue(key);
return valueBox;
}
};
void testMap(Map &m, int i) {
*m.getValue(i) = 1;
#ifndef SUPPRESSED
// expected-warning@-2 {{Dereference of null pointer}}
#endif
*m.getValueIndirectly(i) = 1;
#ifndef SUPPRESSED
// expected-warning@-2 {{Dereference of null pointer}}
#endif
int *&box = m.getValue(i);
extern int *getPointer();
box = getPointer();
*box = 1; // no-warning
int *&box2 = m.getValue(i);
box2 = 0;
*box2 = 1; // expected-warning {{Dereference of null pointer}}
}
SomeClass *&getSomeClass() {
if (coin()) {
extern SomeClass *&opaqueClass();
return opaqueClass();
} else {
static SomeClass *sharedClass;
sharedClass = 0;
return sharedClass;
}
}
void testClass() {
getSomeClass()->doSomething();
#ifndef SUPPRESSED
// expected-warning@-2 {{Called C++ object pointer is null}}
#endif
// Separate the lvalue-to-rvalue conversion from the subsequent dereference.
SomeClass *object = getSomeClass();
object->doSomething();
#ifndef SUPPRESSED
// expected-warning@-2 {{Called C++ object pointer is null}}
#endif
}
SomeClass *getNull() {
return 0;
}
SomeClass &returnNullReference() {
SomeClass *x = getNull();
return *x;
#ifndef SUPPRESSED
// expected-warning@-2 {{Returning null reference}}
#endif
}
}
class X{
public:
void get();
};
X *getNull() {
return 0;
}
void deref1(X *const &p) {
return p->get();
#ifndef SUPPRESSED
// expected-warning@-2 {{Called C++ object pointer is null}}
#endif
}
void test1() {
return deref1(getNull());
}
void deref2(X *p3) {
p3->get();
#ifndef SUPPRESSED
// expected-warning@-2 {{Called C++ object pointer is null}}
#endif
}
void pass2(X *const &p2) {
deref2(p2);
}
void test2() {
pass2(getNull());
}
void deref3(X *const &p2) {
X *p3 = p2;
p3->get();
#ifndef SUPPRESSED
// expected-warning@-2 {{Called C++ object pointer is null}}
#endif
}
void test3() {
deref3(getNull());
}
namespace Cleanups {
class NonTrivial {
public:
~NonTrivial();
SomeClass *getNull() {
return 0;
}
};
void testImmediate() {
NonTrivial().getNull()->doSomething();
#ifndef SUPPRESSED
// expected-warning@-2 {{Called C++ object pointer is null}}
#endif
}
void testAssignment() {
SomeClass *ptr = NonTrivial().getNull();
ptr->doSomething();
#ifndef SUPPRESSED
// expected-warning@-2 {{Called C++ object pointer is null}}
#endif
}
void testArgumentHelper(SomeClass *arg) {
arg->doSomething();
#ifndef SUPPRESSED
// expected-warning@-2 {{Called C++ object pointer is null}}
#endif
}
void testArgument() {
testArgumentHelper(NonTrivial().getNull());
}
}