Use strip_expr() in split_conditions() instead reproducing it badly.
[smatch.git] / check_null_deref.c
blob0fb759c37bb79b31c2f32acbdaf6da56b46112db
1 /*
2 * sparse/check_deference.c
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include "token.h"
11 #include "smatch.h"
13 /*
14 * TODO: The return_null list of functions should be determined automatically
16 const char *return_null[] = {
17 "kmalloc",
20 struct func_n_param {
21 struct symbol *func;
22 int param;
23 int line;
25 ALLOCATOR(func_n_param, "func parameters");
26 DECLARE_PTR_LIST(param_list, struct func_n_param);
28 static struct param_list *funcs;
29 static struct param_list *do_not_call;
30 static struct param_list *calls;
32 static int my_id;
34 STATE(argument);
35 STATE(assumed_nonnull);
36 STATE(ignore);
37 STATE(isnull);
38 STATE(nonnull);
40 static struct symbol *func_sym;
42 static struct smatch_state *merge_func(const char *name, struct symbol *sym,
43 struct smatch_state *s1,
44 struct smatch_state *s2)
46 /*
47 * conditions are a special case. In the cond_(true|false)_stack
48 * we expect to be merging null with a new specified state all the
49 * time. Outside of a condition we have things where the code
50 * assumes a global variable is non null. That gets merged with
51 * other code and it becomes undefined. But really it should be
52 * non-null.
53 * In theory we could test for that latter case by printing a message
54 * when someone checks a variable we already had marked as non-null.
55 * In practise that didn't work really well because a lot of macros
56 * have "unneeded" checks for null.
58 if (!in_condition() && s1 == NULL)
59 return s2;
60 if (s1 == &ignore || s2 == &ignore)
61 return &ignore;
62 if (s1 == NULL && s2 == &assumed_nonnull)
63 return &assumed_nonnull;
64 if (s1 == &assumed_nonnull && s2 == &nonnull)
65 return &assumed_nonnull;
66 if (s1 == &argument && s2 == &assumed_nonnull)
67 return &assumed_nonnull;
68 if (s1 == &argument && s2 == &nonnull)
69 return &nonnull;
70 return &merged;
73 static struct func_n_param *alloc_func_n_param(struct symbol *func, int param,
74 int line)
76 struct func_n_param *tmp = __alloc_func_n_param(0);
78 tmp->func = func;
79 tmp->param = param;
80 tmp->line = line;
81 return tmp;
84 static int get_arg_num(struct symbol *sym)
86 struct symbol *arg;
87 int i = 0;
89 FOR_EACH_PTR(func_sym->ctype.base_type->arguments, arg) {
90 if (arg == sym) {
91 return i;
93 i++;
94 } END_FOR_EACH_PTR(arg);
95 return -1;
98 static void add_do_not_call(struct symbol *sym, int line)
100 struct func_n_param *tmp;
101 int num = get_arg_num(sym);
103 FOR_EACH_PTR(do_not_call, tmp) {
104 if (tmp->func == func_sym && tmp->param == num)
105 return;
106 } END_FOR_EACH_PTR(tmp);
107 tmp = alloc_func_n_param(func_sym, num, line);
108 add_ptr_list(&do_not_call, tmp);
111 static void add_param(struct param_list **list, struct symbol *func, int param,
112 int line)
114 struct func_n_param *tmp;
116 tmp = alloc_func_n_param(func, param, line);
117 add_ptr_list(list, tmp);
120 static void match_function_def(struct symbol *sym)
122 struct symbol *arg;
124 func_sym = sym;
125 add_param(&funcs, sym, 0, 0);
126 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
127 if (!arg->ident) {
128 continue;
130 set_state(arg->ident->name, my_id, arg, &argument);
131 } END_FOR_EACH_PTR(arg);
134 static void match_function_call_after(struct expression *expr)
136 struct expression *tmp;
137 struct symbol *sym;
138 char *name;
139 struct symbol *func = NULL;
140 int i;
142 if (expr->fn->type == EXPR_SYMBOL) {
143 func = expr->fn->symbol;
146 i = 0;
147 FOR_EACH_PTR(expr->args, tmp) {
148 tmp = strip_expr(tmp);
149 if (tmp->op == '&') {
150 name = get_variable_from_expr(tmp->unop, &sym);
151 if (name) {
152 set_state(name, my_id, sym, &assumed_nonnull);
154 } else {
155 name = get_variable_from_expr(tmp, &sym);
156 if (func && name && sym) {
157 if (get_state(name, my_id, sym) == &undefined ||
158 get_state(name, my_id, sym) == &merged)
159 add_param(&calls, func, i, get_lineno());
160 } else
161 free_string(name);
164 i++;
165 } END_FOR_EACH_PTR(tmp);
168 static char *get_function_call(struct expression *expr)
170 if (expr->type != EXPR_CALL)
171 return NULL;
172 if (expr->fn->type == EXPR_SYMBOL && expr->fn->symbol)
173 return expr->fn->symbol->ident->name;
174 return NULL;
177 static int check_null_returns(const char *name, struct symbol *sym,
178 struct expression *right)
180 int i;
181 char *func_name;
183 func_name = get_function_call(right);
184 if (!func_name)
185 return 0;
187 for(i = 0; i < sizeof(*return_null)/sizeof(return_null[0]); i++) {
188 if (!strcmp(func_name,return_null[i])) {
189 set_state(name, my_id, sym, &undefined);
190 return 1;
193 return 0;
196 static int assign_seen;
197 static void match_assign(struct expression *expr)
199 struct expression *left, *right;
200 struct symbol *sym;
201 char *name;
203 if (assign_seen) {
204 assign_seen--;
205 return;
207 left = strip_expr(expr->left);
208 name = get_variable_from_expr(left, &sym);
209 if (!name)
210 return;
211 right = strip_expr(expr->right);
212 if (is_zero(right)) {
213 set_state(name, my_id, sym, &isnull);
214 return;
216 if (check_null_returns(name, sym, right))
217 return;
219 /* by default we assume it's assigned something nonnull */
220 set_state(name, my_id, sym, &assumed_nonnull);
224 * set_new_true_false_states is used in the following conditions
225 * if (a) { ... if (a) { ... } }
226 * The problem is that after the second blog a is set to undefined
227 * even though the second condition is meaning less. (The second test
228 * could be a macro for example).
231 static void set_new_true_false_states(const char *name, int my_id,
232 struct symbol *sym, struct smatch_state *true_state,
233 struct smatch_state *false_state)
235 struct smatch_state *tmp;
237 tmp = get_state(name, my_id, sym);
239 SM_DEBUG("set_new_stuff called at %d value='%s'\n", get_lineno(), show_state(tmp));
241 if (!tmp || tmp == &undefined || tmp == &merged || tmp == &isnull || tmp == &argument)
242 set_true_false_states(name, my_id, sym, true_state, false_state);
245 static void match_condition(struct expression *expr)
247 struct symbol *sym;
248 char *name;
250 expr = strip_expr(expr);
251 switch(expr->type) {
252 case EXPR_PREOP:
253 case EXPR_SYMBOL:
254 case EXPR_DEREF:
255 name = get_variable_from_expr(expr, &sym);
256 if (!name)
257 return;
258 set_new_true_false_states(name, my_id, sym, &nonnull, &isnull);
259 return;
260 case EXPR_ASSIGNMENT:
261 assign_seen++;
263 * There is a kernel macro that does
264 * for ( ... ; ... || x = NULL ; ) ...
266 if (is_zero(expr->right)) {
267 name = get_variable_from_expr(expr->left, &sym);
268 if (!name)
269 return;
270 set_new_true_false_states(name, my_id, sym, NULL, &isnull);
271 return;
273 /* You have to deal with stuff like if (a = b = c) */
274 match_condition(expr->right);
275 match_condition(expr->left);
276 return;
277 default:
278 return;
282 static void match_declarations(struct symbol *sym)
284 const char *name;
286 if ((get_base_type(sym))->type == SYM_ARRAY) {
287 return;
290 name = sym->ident->name;
292 if (sym->initializer) {
293 if (is_zero(sym->initializer)) {
294 set_state(name, my_id, sym, &isnull);
295 return;
297 if (check_null_returns(name, sym, strip_expr(sym->initializer)))
298 return;
299 set_state(name, my_id, sym, &assumed_nonnull);
300 } else {
301 set_state(name, my_id, sym, &undefined);
305 static void match_dereferences(struct expression *expr)
307 char *deref = NULL;
308 struct symbol *sym = NULL;
309 struct smatch_state *state;
311 if (strcmp(show_special(expr->deref->op), "*"))
312 return;
314 deref = get_variable_from_expr(expr->deref->unop, &sym);
315 if (!deref)
316 return;
318 state = get_state(deref, my_id, sym);
319 if (state == &undefined || state == &merged) {
320 smatch_msg("Dereferencing Undefined: '%s'", deref);
321 set_state(deref, my_id, sym, &ignore);
322 } else if (state == &isnull) {
324 * It turns out that you only get false positives from
325 * this. Mostly foo = NULL; sizeof(*foo);
326 * And even if it wasn't always a false positive you'd think
327 * it would get caught in testing if it failed every time.
329 set_state(deref, my_id, sym, &ignore);
330 } else if (state == &argument) {
331 add_do_not_call(sym, get_lineno());
332 set_state(deref, my_id, sym, &assumed_nonnull);
333 } else {
334 free_string(deref);
338 static void end_file_processing()
340 struct func_n_param *param1, *param2;
342 // if a function is not static print it out...
343 FOR_EACH_PTR(do_not_call, param1) {
344 if (!(param1->func->ctype.modifiers & MOD_STATIC))
345 printf("%s +%d unchecked param %s %d\n",
346 get_filename(), param1->line,
347 param1->func->ident->name, param1->param);
348 } END_FOR_EACH_PTR(param1);
350 // if there is an error print it out...
351 FOR_EACH_PTR(calls, param1) {
352 FOR_EACH_PTR(do_not_call, param2) {
353 if (param1->func == param2->func &&
354 param1->param == param2->param)
355 printf("%s +%d cross_func deref %s %d\n",
356 get_filename(), param1->line,
357 param1->func->ident->name,
358 param1->param);
359 } END_FOR_EACH_PTR(param2);
360 } END_FOR_EACH_PTR(param1);
362 // if someone calls a non-static function print that..
363 FOR_EACH_PTR(calls, param1) {
364 int defined = 0;
366 FOR_EACH_PTR(funcs, param2) {
367 if (param1->func == param2->func)
368 defined = 1;
369 } END_FOR_EACH_PTR(param2);
370 if (!defined)
371 printf("%s +%d undefined param %s %d\n", get_filename(),
372 param1->line, param1->func->ident->name,
373 param1->param);
374 } END_FOR_EACH_PTR(param1);
377 void register_null_deref(int id)
379 my_id = id;
380 add_merge_hook(my_id, &merge_func);
381 add_hook(&match_function_def, FUNC_DEF_HOOK);
382 add_hook(&match_function_call_after, FUNCTION_CALL_AFTER_HOOK);
383 add_hook(&match_assign, ASSIGNMENT_AFTER_HOOK);
384 add_hook(&match_condition, CONDITION_HOOK);
385 add_hook(&match_dereferences, DEREF_HOOK);
386 add_hook(&match_declarations, DECLARATION_HOOK);
387 add_hook(&end_file_processing, END_FILE_HOOK);