extra: handle "if (a - 3 > 100) "
[smatch.git] / check_dereferences_param.c
blob6b69ff50e55b7f63adb1a3c365b2de8654c2f681
1 /*
2 * sparse/check_dereferences_param.c
4 * Copyright (C) 2012 Oracle.
6 * Licensed under the Open Software License version 1.1
8 */
11 * This is an --info recipe. The goal is to print a message for every parameter
12 * which we can not avoid dereferencing. This is maybe a bit restrictive but it
13 * avoids some false positives.
16 #include "smatch.h"
17 #include "smatch_extra.h"
18 #include "smatch_slist.h"
20 static int my_id;
22 STATE(derefed);
23 STATE(ignore);
24 STATE(param);
26 static int is_arg(struct expression *expr)
28 struct symbol *arg;
30 if (expr->type != EXPR_SYMBOL)
31 return 0;
33 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
34 if (arg == expr->symbol)
35 return 1;
36 } END_FOR_EACH_PTR(arg);
37 return 0;
40 static void set_ignore(struct sm_state *sm)
42 if (sm->state == &derefed)
43 return;
44 set_state(my_id, sm->name, sm->sym, &ignore);
47 static void match_function_def(struct symbol *sym)
49 struct symbol *arg;
50 int i;
52 i = -1;
53 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
54 i++;
55 if (!arg->ident)
56 continue;
57 set_state(my_id, arg->ident->name, arg, &param);
58 } END_FOR_EACH_PTR(arg);
61 static void check_deref(struct expression *expr)
63 struct expression *tmp;
64 struct sm_state *sm;
66 tmp = get_assigned_expr(expr);
67 if (tmp)
68 expr = tmp;
69 expr = strip_expr(expr);
71 if (!is_arg(expr))
72 return;
74 sm = get_sm_state_expr(my_id, expr);
75 if (sm && slist_has_state(sm->possible, &ignore))
76 return;
77 set_state_expr(my_id, expr, &derefed);
80 static void match_dereference(struct expression *expr)
82 if (expr->type != EXPR_PREOP)
83 return;
84 if (getting_address())
85 return;
86 check_deref(expr->unop);
89 static void set_param_dereferenced(struct expression *arg, char *unused)
91 check_deref(arg);
94 static void process_states(struct state_list *slist)
96 struct symbol *arg;
97 int i;
99 i = -1;
100 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
101 i++;
102 if (!arg->ident)
103 continue;
104 if (get_state_slist(slist, my_id, arg->ident->name, arg) == &derefed)
105 sql_insert_call_implies(DEREFERENCE, i, 1);
106 } END_FOR_EACH_PTR(arg);
109 void check_dereferences_param(int id)
111 my_id = id;
113 add_hook(&match_function_def, FUNC_DEF_HOOK);
115 add_hook(&match_dereference, DEREF_HOOK);
116 add_db_fn_call_callback(DEREFERENCE, &set_param_dereferenced);
117 add_modification_hook(my_id, &set_ignore);
119 all_return_states_hook(&process_states);