conditions: handle comma condtions
[smatch.git] / check_frees_param.c
blob9d6577db94c4b3370ea71936496af342c7e758a6
1 /*
2 * Copyright (C) 2014 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 file is sort of like check_dereferences_param.c. In theory the one
20 * difference should be that the param is NULL it should still be counted as a
21 * free. But for now I don't handle that case.
24 #include "smatch.h"
25 #include "smatch_extra.h"
26 #include "smatch_slist.h"
28 static int my_id;
30 STATE(freed);
31 STATE(ignore);
32 STATE(param);
34 static void set_ignore(struct sm_state *sm, struct expression *mod_expr)
36 if (sm->state == &freed)
37 return;
38 set_state(my_id, sm->name, sm->sym, &ignore);
41 static void match_function_def(struct symbol *sym)
43 struct symbol *arg;
44 int i;
46 i = -1;
47 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
48 i++;
49 if (!arg->ident)
50 continue;
51 set_state(my_id, arg->ident->name, arg, &param);
52 } END_FOR_EACH_PTR(arg);
55 static void freed_variable(struct expression *expr)
57 struct sm_state *sm;
59 expr = strip_expr(expr);
60 if (get_param_num(expr) < 0)
61 return;
63 sm = get_sm_state_expr(my_id, expr);
64 if (sm && slist_has_state(sm->possible, &ignore))
65 return;
66 set_state_expr(my_id, expr, &freed);
69 static void match_free(const char *fn, struct expression *expr, void *param)
71 struct expression *arg;
73 arg = get_argument_from_call_expr(expr->args, PTR_INT(param));
74 if (!arg)
75 return;
76 freed_variable(arg);
79 static void set_param_freed(struct expression *arg, char *key, char *unused)
81 /* XXX FIXME: call_implies has been updated with more information */
82 if (strcmp(key, "$") != 0)
83 return;
84 freed_variable(arg);
87 static void process_states(struct stree *stree)
89 struct sm_state *sm;
90 int param;
91 const char *param_name;
93 FOR_EACH_MY_SM(my_id, stree, sm) {
94 if (sm->state != &freed)
95 continue;
96 param = get_param_num_from_sym(sm->sym);
97 if (param < 0)
98 continue;
99 param_name = get_param_name(sm);
100 if (!param_name)
101 continue;
102 sql_insert_call_implies(PARAM_FREED, param, param_name, "1");
103 } END_FOR_EACH_SM(sm);
107 void check_frees_param(int id)
109 my_id = id;
111 add_hook(&match_function_def, FUNC_DEF_HOOK);
113 if (option_project == PROJ_KERNEL) {
114 add_function_hook("kfree", &match_free, INT_PTR(0));
115 add_function_hook("kmem_cache_free", &match_free, INT_PTR(1));
116 } else {
117 add_function_hook("free", &match_free, INT_PTR(0));
120 select_call_implies_hook(PARAM_FREED, &set_param_freed);
121 add_modification_hook(my_id, &set_ignore);
123 all_return_states_hook(&process_states);