implied: smatch was saying somethings were implied when they weren't
[smatch.git] / check_dereferences_param.c
blob3317390f687cba752a4a39c0c51c283beb7522cd
1 /*
2 * Copyright (C) 2012 Oracle.
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 * This is an --info recipe. The goal is to print a message for every parameter
20 * which we can not avoid dereferencing. This is maybe a bit restrictive but it
21 * avoids some false positives.
24 #include "smatch.h"
25 #include "smatch_extra.h"
26 #include "smatch_slist.h"
28 static int my_id;
30 STATE(derefed);
31 STATE(ignore);
32 STATE(param);
34 static int is_arg(struct expression *expr)
36 struct symbol *arg;
38 if (expr->type != EXPR_SYMBOL)
39 return 0;
41 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
42 if (arg == expr->symbol)
43 return 1;
44 } END_FOR_EACH_PTR(arg);
45 return 0;
48 static void set_ignore(struct sm_state *sm, struct expression *mod_expr)
50 if (sm->state == &derefed)
51 return;
52 set_state(my_id, sm->name, sm->sym, &ignore);
55 static void match_function_def(struct symbol *sym)
57 struct symbol *arg;
58 int i;
60 i = -1;
61 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
62 i++;
63 if (!arg->ident)
64 continue;
65 set_state(my_id, arg->ident->name, arg, &param);
66 } END_FOR_EACH_PTR(arg);
69 static void check_deref(struct expression *expr)
71 struct expression *tmp;
72 struct sm_state *sm;
74 tmp = get_assigned_expr(expr);
75 if (tmp)
76 expr = tmp;
77 expr = strip_expr(expr);
79 if (!is_arg(expr))
80 return;
82 sm = get_sm_state_expr(my_id, expr);
83 if (sm && slist_has_state(sm->possible, &ignore))
84 return;
85 set_state_expr(my_id, expr, &derefed);
88 static void match_dereference(struct expression *expr)
90 if (expr->type != EXPR_PREOP)
91 return;
92 if (getting_address())
93 return;
94 check_deref(expr->unop);
97 static void set_param_dereferenced(struct expression *arg, char *unused)
99 check_deref(arg);
102 static void process_states(struct stree *stree)
104 struct symbol *arg;
105 int i;
107 i = -1;
108 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
109 i++;
110 if (!arg->ident)
111 continue;
112 if (get_state_stree(stree, my_id, arg->ident->name, arg) == &derefed)
113 sql_insert_call_implies(DEREFERENCE, i, 1);
114 } END_FOR_EACH_PTR(arg);
117 void check_dereferences_param(int id)
119 my_id = id;
121 add_hook(&match_function_def, FUNC_DEF_HOOK);
123 add_hook(&match_dereference, DEREF_HOOK);
124 select_call_implies_hook(DEREFERENCE, &set_param_dereferenced);
125 add_modification_hook(my_id, &set_ignore);
127 all_return_states_hook(&process_states);