forked from error27/smatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_checking_for_null_instead_of_err_ptr.c
186 lines (156 loc) · 4.11 KB
/
check_checking_for_null_instead_of_err_ptr.c
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
/*
* Copyright (C) 2017 Oracle.
*
* 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 2
* of the License, or (at your option) 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/copyleft/gpl.txt
*/
#include "smatch.h"
#include "smatch_slist.h"
#include "smatch_extra.h"
static int my_id;
STATE(err_ptr);
static struct string_list *ignored_macros;
static int in_ignored_macro(struct position pos)
{
const char *macro;
char *tmp;
macro = get_macro_name(pos);
if (!macro)
return 0;
FOR_EACH_PTR(ignored_macros, tmp) {
if (!strcmp(tmp, macro))
return 1;
} END_FOR_EACH_PTR(tmp);
return 0;
}
static void ok_to_use(struct sm_state *sm, struct expression *mod_expr)
{
set_state(my_id, sm->name, sm->sym, &undefined);
}
static void match_returns_err_ptr(const char *fn, struct expression *expr,
void *info)
{
set_state_expr(my_id, expr->left, &err_ptr);
}
static void match_condition(struct expression *expr)
{
struct sm_state *sm;
struct range_list *rl;
sval_t zero = {
.type = NULL,
{ .value = 0, }
};
char *name;
struct range_list *err_rl;
while (expr->type == EXPR_ASSIGNMENT)
expr = strip_expr(expr->left);
if (!is_pointer(expr))
return;
sm = get_sm_state_expr(my_id, expr);
if (!sm)
return;
if (!slist_has_state(sm->possible, &err_ptr))
return;
if (!get_implied_rl(expr, &rl))
return;
zero.type = rl_type(rl);
if (rl_has_sval(rl, zero))
return;
/*
* At this point we already know that the condition is bogus because
* it's non-NULL. But let's do another filter to make sure it really is
* a possible error pointer.
*
*/
err_rl = alloc_rl(err_min, err_max);
if (!possibly_true_rl(rl, SPECIAL_EQUAL, err_rl))
return;
if (in_ignored_macro(expr->pos))
return;
name = expr_to_str(expr);
sm_msg("warn: '%s' is an error pointer or valid", name);
free_string(name);
}
static void match_condition2(struct expression *expr)
{
struct range_list *rl;
struct data_range *drange;
char *name;
if (!is_pointer(expr))
return;
if (!get_implied_rl(expr, &rl))
return;
FOR_EACH_PTR(rl, drange) {
if (sval_cmp(drange->min, drange->max) != 0)
continue;
if (drange->min.value >= -4095 && drange->min.value < 0)
goto warn;
} END_FOR_EACH_PTR(drange);
return;
warn:
name = expr_to_str(expr);
if (option_spammy)
sm_warning("'%s' could be an error pointer", name);
free_string(name);
}
static void register_err_ptr_funcs(void)
{
struct token *token;
const char *func;
token = get_tokens_file("kernel.returns_err_ptr");
if (!token)
return;
if (token_type(token) != TOKEN_STREAMBEGIN)
return;
token = token->next;
while (token_type(token) != TOKEN_STREAMEND) {
if (token_type(token) != TOKEN_IDENT)
return;
func = show_ident(token->ident);
add_function_assign_hook(func, &match_returns_err_ptr, NULL);
token = token->next;
}
clear_token_alloc();
}
static void register_ignored_macros(void)
{
struct token *token;
char *macro;
char name[256];
snprintf(name, 256, "%s.ignore_bogus_null_checks", option_project_str);
token = get_tokens_file(name);
if (!token)
return;
if (token_type(token) != TOKEN_STREAMBEGIN)
return;
token = token->next;
while (token_type(token) != TOKEN_STREAMEND) {
if (token_type(token) != TOKEN_IDENT)
return;
macro = alloc_string(show_ident(token->ident));
add_ptr_list(&ignored_macros, macro);
token = token->next;
}
clear_token_alloc();
}
void check_checking_for_null_instead_of_err_ptr(int id)
{
if (option_project != PROJ_KERNEL)
return;
my_id = id;
register_err_ptr_funcs();
add_hook(&match_condition, CONDITION_HOOK);
add_hook(&match_condition2, CONDITION_HOOK);
add_modification_hook(my_id, &ok_to_use);
register_ignored_macros();
}