db: use the load_strings_from_file() helper function
[smatch.git] / check_frees_param.c
blob945d969909b30ede2f0b3926bc13230a97fc7d3d
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(maybe_freed);
32 STATE(ignore);
34 static void set_ignore(struct sm_state *sm, struct expression *mod_expr)
36 set_state(my_id, sm->name, sm->sym, &ignore);
39 static void freed_variable(struct expression *expr)
41 struct sm_state *sm;
43 expr = strip_expr(expr);
44 if (get_param_num(expr) < 0)
45 return;
47 sm = get_sm_state_expr(my_id, expr);
48 if (sm && slist_has_state(sm->possible, &ignore))
49 return;
50 set_state_expr(my_id, expr, &freed);
53 static void match_free(const char *fn, struct expression *expr, void *param)
55 struct expression *arg;
57 arg = get_argument_from_call_expr(expr->args, PTR_INT(param));
58 if (!arg)
59 return;
60 freed_variable(arg);
63 static void set_param_freed(struct expression *call, struct expression *arg, char *key, char *unused)
65 /* XXX FIXME: return_implies has been updated with more information */
66 if (strcmp(key, "$") != 0)
67 return;
68 freed_variable(arg);
71 static void process_states(void)
73 struct sm_state *sm;
74 int param;
75 const char *param_name;
77 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
78 // FIXME: how can this be maybe_freed?
79 if (sm->state != &freed && sm->state != &maybe_freed)
80 continue;
81 param = get_param_num_from_sym(sm->sym);
82 if (param < 0)
83 continue;
84 param_name = get_param_name(sm);
85 if (!param_name)
86 continue;
87 sql_insert_return_implies(
88 (sm->state == &freed) ? PARAM_FREED : MAYBE_FREED,
89 param, param_name, "1");
90 } END_FOR_EACH_SM(sm);
94 void check_frees_param(int id)
96 my_id = id;
98 if (option_project == PROJ_KERNEL) {
99 /* The kernel uses check_frees_param_strict.c */
100 return;
103 add_function_hook("free", &match_free, INT_PTR(0));
105 select_return_implies_hook(PARAM_FREED, &set_param_freed);
106 add_modification_hook(my_id, &set_ignore);
108 all_return_states_hook(&process_states);