unreachable: don't complain about iterate_all_kinds()
[smatch.git] / check_deref.c
blobddcb03c8bb850cef43381e2fac6bf71c61e22dff
1 /*
2 * Copyright (C) 2010 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * There was a previous null dereference test but it was too confusing and
20 * difficult to debug. This test is much simpler in its goals and scope.
22 * This test only complains about:
23 * 1) dereferencing uninitialized variables
24 * 2) dereferencing variables which were assigned as null.
25 * 3) dereferencing variables which were assigned a function the returns
26 * null.
28 * If we dereference something then we complain if any of those three
29 * are possible.
33 #include "smatch.h"
34 #include "smatch_slist.h"
35 #include "smatch_extra.h"
37 static int my_id;
39 #define __GFP_NOFAIL 0x800
41 STATE(null);
42 STATE(ok);
43 STATE(uninitialized);
45 static struct smatch_state *alloc_my_state(const char *name)
47 struct smatch_state *state;
49 state = __alloc_smatch_state(0);
50 state->name = name;
51 return state;
54 static struct smatch_state *unmatched_state(struct sm_state *sm)
56 return &ok;
59 static void is_ok(struct sm_state *sm, struct expression *mod_expr)
61 set_state(my_id, sm->name, sm->sym, &ok);
64 static void check_dereference(struct expression *expr)
66 struct sm_state *sm;
67 struct sm_state *tmp;
69 expr = strip_expr(expr);
70 sm = get_sm_state_expr(my_id, expr);
71 if (!sm)
72 return;
73 if (is_ignored(my_id, sm->name, sm->sym))
74 return;
75 if (implied_not_equal(expr, 0))
76 return;
77 if (is_impossible_path())
78 return;
80 FOR_EACH_PTR(sm->possible, tmp) {
81 if (tmp->state == &merged)
82 continue;
83 if (tmp->state == &ok)
84 continue;
85 add_ignore(my_id, sm->name, sm->sym);
86 if (tmp->state == &null) {
87 if (option_spammy)
88 sm_msg("error: potential NULL dereference '%s'.", tmp->name);
89 return;
91 if (tmp->state == &uninitialized) {
92 if (option_spammy)
93 sm_msg("error: potentially dereferencing uninitialized '%s'.", tmp->name);
94 return;
96 sm_msg("error: potential null dereference '%s'. (%s returns null)",
97 tmp->name, tmp->state->name);
98 return;
99 } END_FOR_EACH_PTR(tmp);
102 static void check_dereference_name_sym(char *name, struct symbol *sym)
104 struct sm_state *sm;
105 struct sm_state *tmp;
107 sm = get_sm_state(my_id, name, sym);
108 if (!sm)
109 return;
110 if (is_ignored(my_id, sm->name, sm->sym))
111 return;
112 if (implied_not_equal_name_sym(name, sym, 0))
113 return;
114 if (is_impossible_path())
115 return;
117 FOR_EACH_PTR(sm->possible, tmp) {
118 if (tmp->state == &merged)
119 continue;
120 if (tmp->state == &ok)
121 continue;
122 add_ignore(my_id, sm->name, sm->sym);
123 if (tmp->state == &null) {
124 if (option_spammy)
125 sm_msg("error: potential NULL dereference '%s'.", tmp->name);
126 return;
128 if (tmp->state == &uninitialized) {
129 if (option_spammy)
130 sm_msg("error: potentially dereferencing uninitialized '%s'.", tmp->name);
131 return;
133 sm_msg("error: potential null dereference '%s'. (%s returns null)",
134 tmp->name, tmp->state->name);
135 return;
136 } END_FOR_EACH_PTR(tmp);
139 static void match_dereferences(struct expression *expr)
141 if (expr->type != EXPR_PREOP)
142 return;
143 check_dereference(expr->unop);
146 static void match_pointer_as_array(struct expression *expr)
148 if (!is_array(expr))
149 return;
150 check_dereference(get_array_base(expr));
153 static void set_param_dereferenced(struct expression *arg, char *key, char *unused)
155 struct symbol *sym;
156 char *name;
158 name = get_variable_from_key(arg, key, &sym);
159 if (!name || !sym)
160 goto free;
162 check_dereference_name_sym(name, sym);
163 free:
164 free_string(name);
167 static void match_declarations(struct symbol *sym)
169 const char *name;
171 if ((get_base_type(sym))->type == SYM_ARRAY)
172 return;
174 if (!sym->ident)
175 return;
176 name = sym->ident->name;
177 if (!sym->initializer) {
178 set_state(my_id, name, sym, &uninitialized);
179 scoped_state(my_id, name, sym);
183 static void match_assign(struct expression *expr)
185 struct statement *stmt;
187 if (!is_zero(expr->right))
188 return;
190 if (__in_fake_assign)
191 return;
193 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
194 if (stmt->type == STMT_DECLARATION)
195 return;
196 break;
197 } END_FOR_EACH_PTR_REVERSE(stmt);
199 set_state_expr(my_id, expr->left, &null);
202 static void match_condition(struct expression *expr)
204 if (expr->type == EXPR_ASSIGNMENT) {
205 match_condition(expr->right);
206 match_condition(expr->left);
208 if (!get_state_expr(my_id, expr))
209 return;
210 set_true_false_states_expr(my_id, expr, &ok, NULL);
213 static int called_with_no_fail(struct expression *call, int param)
215 struct expression *arg;
216 sval_t sval;
218 if (param == -1)
219 return 0;
220 call = strip_expr(call);
221 if (call->type != EXPR_CALL)
222 return 0;
223 arg = get_argument_from_call_expr(call->args, param);
224 if (get_value(arg, &sval) && (sval.uvalue & __GFP_NOFAIL))
225 return 1;
226 return 0;
229 static void match_assign_returns_null(const char *fn, struct expression *expr, void *_gfp)
231 struct smatch_state *state;
232 int gfp_param = PTR_INT(_gfp);
234 if (called_with_no_fail(expr->right, gfp_param))
235 return;
236 state = alloc_my_state(fn);
237 set_state_expr(my_id, expr->left, state);
240 static void register_allocation_funcs(void)
242 struct token *token;
243 const char *func;
244 int arg;
246 token = get_tokens_file("kernel.allocation_funcs_gfp");
247 if (!token)
248 return;
249 if (token_type(token) != TOKEN_STREAMBEGIN)
250 return;
251 token = token->next;
252 while (token_type(token) != TOKEN_STREAMEND) {
253 if (token_type(token) != TOKEN_IDENT)
254 return;
255 func = show_ident(token->ident);
256 token = token->next;
257 if (token_type(token) == TOKEN_IDENT)
258 arg = -1;
259 else if (token_type(token) == TOKEN_NUMBER)
260 arg = atoi(token->number);
261 else
262 return;
263 add_function_assign_hook(func, &match_assign_returns_null, INT_PTR(arg));
264 token = token->next;
266 clear_token_alloc();
269 void check_deref(int id)
271 my_id = id;
273 add_unmatched_state_hook(my_id, &unmatched_state);
274 add_modification_hook(my_id, &is_ok);
275 add_hook(&match_dereferences, DEREF_HOOK);
276 add_hook(&match_pointer_as_array, OP_HOOK);
277 select_call_implies_hook(DEREFERENCE, &set_param_dereferenced);
278 add_hook(&match_condition, CONDITION_HOOK);
279 add_hook(&match_declarations, DECLARATION_HOOK);
280 add_hook(&match_assign, ASSIGNMENT_HOOK);
281 if (option_project == PROJ_KERNEL)
282 register_allocation_funcs();