comparison: call simplify_binops() in get_comparison_helper()
[smatch.git] / check_dereferences_param.c
blobff9636baf084259322b513a8d162edb91380a696
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 void pre_merge_hook(struct sm_state *cur, struct sm_state *other)
36 if (cur->state == &derefed || other->state != &derefed)
37 return;
38 if (is_impossible_path())
39 set_state(my_id, cur->name, cur->sym, &derefed);
42 static void set_ignore(struct sm_state *sm, struct expression *mod_expr)
44 if (sm->state == &derefed)
45 return;
46 set_state(my_id, sm->name, sm->sym, &ignore);
49 static void match_function_def(struct symbol *sym)
51 struct symbol *arg;
52 int i;
54 i = -1;
55 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
56 i++;
57 if (!arg->ident)
58 continue;
59 set_state(my_id, arg->ident->name, arg, &param);
60 } END_FOR_EACH_PTR(arg);
63 static int is_ignored_param(struct expression *expr)
65 struct sm_state *sm;
67 if (param_was_set(expr))
68 return 1;
70 sm = get_sm_state_expr(my_id, expr);
71 if (sm && slist_has_state(sm->possible, &ignore))
72 return 1;
73 return 0;
76 static void check_deref(struct expression *expr)
78 struct expression *tmp;
80 if (is_impossible_path())
81 return;
83 tmp = get_assigned_expr(expr);
84 if (tmp)
85 expr = tmp;
87 if (expr->type == EXPR_PREOP &&
88 expr->op == '&') {
89 expr = strip_expr(expr->unop);
90 if (expr->type != EXPR_DEREF)
91 return;
92 expr = strip_expr(expr->deref);
93 if (expr->type != EXPR_PREOP ||
94 expr->op != '*')
95 return;
96 expr = strip_expr(expr->unop);
99 expr = strip_expr(expr);
100 if (!expr)
101 return;
103 if (expr->type == EXPR_PREOP && expr->op == '&')
104 return;
106 if (get_param_num(expr) < 0)
107 return;
109 if (is_ignored_param(expr))
110 return;
112 if (param_was_set(expr))
113 return;
116 * At this point we really only care about potential NULL dereferences.
117 * Potentially in the future we will care about everything.
119 if (implied_not_equal(expr, 0))
120 return;
122 set_state_expr(my_id, expr, &derefed);
125 static void match_dereference(struct expression *expr)
127 if (expr->type != EXPR_PREOP)
128 return;
129 check_deref(expr->unop);
132 static void find_inner_dereferences(struct expression *expr)
134 while (expr->type == EXPR_PREOP) {
135 if (expr->op == '*')
136 check_deref(expr->unop);
137 expr = strip_expr(expr->unop);
141 static void set_param_dereferenced(struct expression *call, struct expression *arg, char *key, char *unused)
143 struct symbol *sym;
144 char *name;
146 check_deref(arg);
148 name = get_variable_from_key(arg, key, &sym);
149 if (!name || !sym)
150 goto free;
151 if (is_ignored_param(symbol_expression(sym)))
152 goto free;
153 if (get_param_num_from_sym(sym) < 0)
154 goto free;
155 if (param_was_set_var_sym(name, sym))
156 goto free;
158 set_state(my_id, name, sym, &derefed);
159 find_inner_dereferences(arg);
160 free:
161 free_string(name);
164 static void process_states(void)
166 struct sm_state *tmp;
167 int arg;
168 const char *name;
170 FOR_EACH_MY_SM(my_id, __get_cur_stree(), tmp) {
171 if (tmp->state != &derefed)
172 continue;
173 arg = get_param_num_from_sym(tmp->sym);
174 if (arg < 0)
175 continue;
176 name = get_param_name(tmp);
177 if (!name || name[0] == '&')
178 continue;
179 sql_insert_return_implies(DEREFERENCE, arg, name, "1");
180 } END_FOR_EACH_SM(tmp);
183 static void match_pointer_as_array(struct expression *expr)
185 if (!is_array(expr))
186 return;
187 check_deref(get_array_base(expr));
190 void check_dereferences_param(int id)
192 my_id = id;
194 add_hook(&match_function_def, FUNC_DEF_HOOK);
196 add_hook(&match_dereference, DEREF_HOOK);
197 add_hook(&match_pointer_as_array, OP_HOOK);
198 select_return_implies_hook(DEREFERENCE, &set_param_dereferenced);
199 add_modification_hook(my_id, &set_ignore);
200 add_pre_merge_hook(my_id, &pre_merge_hook);
202 all_return_states_hook(&process_states);