scoped cleanup: update because of sound/ changes
[smatch.git] / check_frees_param_strict.c
blobfe802306f681986cf0e2b9b7067e76afcefe55e8
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(ignore);
32 static struct smatch_state *unmatched_state(struct sm_state *sm)
34 if (parent_is_null_var_sym(sm->name, sm->sym))
35 return sm->state;
36 return &undefined;
39 static void set_ignore(struct sm_state *sm, struct expression *mod_expr)
41 if (sm->sym && sm->sym->ident &&
42 strcmp(sm->sym->ident->name, sm->name) == 0)
43 return;
44 set_state(my_id, sm->name, sm->sym, &ignore);
47 void track_freed_param(struct expression *expr, struct smatch_state *state)
49 const char *key;
50 int param;
52 param = get_param_key_from_expr(expr, NULL, &key);
53 if (param < 0)
54 return;
55 if (param_was_set(expr))
56 return;
58 set_state_expr(my_id, expr, state);
61 void track_freed_param_var_sym(const char *name, struct symbol *sym,
62 struct smatch_state *state)
64 struct expression *tmp;
66 tmp = get_assigned_expr_name_sym(name, sym);
67 if (tmp)
68 track_freed_param(tmp, state);
70 if (get_param_num_from_sym(sym) < 0)
71 return;
73 * FIXME: some unpublished code made this apply to only '$' and I
74 * don't remember why I would do that. But there was probably
75 * a reason.
77 if (param_was_set_var_sym(name, sym))
78 return;
80 set_state(my_id, name, sym, state);
83 static int get_freed_type(struct sm_state *sm)
85 struct sm_state *tmp;
87 if (strcmp(sm->state->name, "freed") == 0)
88 return PARAM_FREED;
90 FOR_EACH_PTR(sm->possible, tmp) {
91 if (strcmp(tmp->state->name, "freed") == 0)
92 return MAYBE_FREED;
93 } END_FOR_EACH_PTR(tmp);
95 return 0;
98 static void return_freed_callback(int return_id, char *return_ranges,
99 struct expression *returned_expr,
100 int param,
101 const char *printed_name,
102 struct sm_state *sm)
104 int type;
106 type = get_freed_type(sm);
107 if (!type)
108 return;
110 if (on_atomic_dec_path())
111 return;
113 sql_insert_return_states(return_id, return_ranges, type,
114 param, printed_name, "");
117 void check_frees_param_strict(int id)
119 my_id = id;
121 add_modification_hook(my_id, &set_ignore);
122 add_return_info_callback(my_id, return_freed_callback);
123 add_unmatched_state_hook(my_id, &unmatched_state);