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
213
214
215
216
|
/*
* Copyright (C) 2015 Andrei Karas
*
* This file is part of Paranoid null checker.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "analysis/reports.h"
#include "command.h"
#include "logger.h"
#include "analysis/analysis.h"
#include "analysis/expression.h"
#include "analysis/walkitem.h"
#include "nodes/base/node.h"
#include "nodes/decl/parm_decl.h"
#include "nodes/decl/var_decl.h"
#include "localconsts.h"
namespace Analysis
{
// check is var from node can be checked for null pointer
bool checkForReport(Node *node,
const WalkItem &wi)
{
node = skipNop(node);
return node &&
(node == PARM_DECL || node == VAR_DECL) &&
isIn(node->label, wi.needCheckNullVars);
}
// report about useless check for null pointer
void reportUselessCheck(Node *node,
const std::string &var)
{
if (!checkCommand(DetectUseless))
return;
Log::warn(findBackLocation(node),
"Useless variable check '%s'. It already was checked before",
var);
}
// report about null pointer if need for node
void reportParmDeclNullPointer(Node *mainNode,
Node *node,
const WalkItem &wi)
{
node = skipNop(node);
if (node)
{
if (!node->label.empty())
{
if (node == PARM_DECL)
{
ParmDeclNode *parmDecl = static_cast<ParmDeclNode*>(node);
if (parmDecl->declType == POINTER_TYPE &&
isIn(node->label, wi.needCheckNullVars))
{
Log::warn(findBackLocation(mainNode),
"Using parameter '%s' without checking for null pointer",
node->label);
}
}
else if (node == VAR_DECL)
{
VarDeclNode *varDecl = static_cast<VarDeclNode*>(node);
if (varDecl->varType == POINTER_TYPE &&
isIn(node->label, wi.needCheckNullVars))
{
Log::warn(findBackLocation(mainNode),
"Using variable '%s' without checking for null pointer",
node->label);
}
}
}
else if (node == COMPONENT_REF)
{
auto vars = getComponentRefParts(node);
FOR_EACH (var, vars)
{
if (isIn(var, wi.needCheckNullVars) ||
isNotIn(var, wi.knownVars))
{
Log::warn(findBackLocation(mainNode),
"Using variable '%s' without checking for null pointer",
var);
}
}
}
}
}
// report about null pointer if need for node
void reportParmDeclLeftNullPointer(Node *mainNode,
Node *node,
const WalkItem &wi)
{
node = skipNop(node);
if (node)
{
if (node == COMPONENT_REF)
{
auto vars = getComponentRefLeftParts(node);
FOR_EACH (var, vars)
{
if (isIn(var, wi.needCheckNullVars))
{
Log::warn(findBackLocation(mainNode),
"Using field '%s' without checking for null pointer",
var);
}
}
}
}
}
// report about null pointer if need for node
void reportComponentRefNullPointer(Node *mainNode,
Node *node,
const WalkItem &wi)
{
node = skipNop(node);
if (node)
{
if (node == COMPONENT_REF)
{
auto vars = getComponentRefParts(node);
FOR_EACH (var, vars)
{
if (isIn(var, wi.needCheckNullVars))
{
Log::warn(findBackLocation(mainNode),
"Using field '%s' without checking for null pointer",
var);
}
}
}
}
}
void reportPossibleNullPointer(Node *node,
const std::string &label)
{
Log::warn(findBackLocation(node),
"warning: possible null argument '%s' where non-null required",
label);
}
void reportParmDeclAttrNullPointer(Node *mainNode,
Node *node,
const WalkItem &wi)
{
node = skipNop(node);
if (node)
{
if (!node->label.empty())
{
if (node == PARM_DECL)
{
if (isIn(node->label, wi.needCheckNullVars))
reportPossibleNullPointer(mainNode, node->label);
}
else if (node == VAR_DECL)
{
if (isIn(node->label, wi.needCheckNullVars))
reportPossibleNullPointer(mainNode, node->label);
}
}
else if (node == COMPONENT_REF)
{
std::string var = getComponentRefVariable(node);
if (isIn(var, wi.needCheckNullVars))
reportPossibleNullPointer(mainNode, node->label);
}
}
}
void reportWrongCheck(Node *node)
{
Log::warn(findBackLocation(node),
"warning: wrong call to internal debug function. Three arguments required",
"");
}
void reportCollectionsDifferent(Node *node,
const std::string &name,
std::string str1,
std::string str2)
{
std::string str = "internal collections '%s' is different.\nwant: " +
str2 +
"\n get: " +
str1;
Log::warn(findBackLocation(node),
str,
name);
}
}
|