2 * Copyright (C) 2010 Dan Carpenter.
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 * The point of this check is to look for leaks.
20 * foo = malloc(); // <- mark it as allocated.
21 * A variable becomes &ok if we:
22 * 1) assign it to another variable.
23 * 2) pass it to a function.
25 * One complication is dealing with stuff like:
26 * foo->bar = malloc();
27 * foo->baz = malloc();
30 * The work around is that for now what this check only
31 * checks simple expressions and doesn't check whether
40 #include "smatch_slist.h"
47 static void set_parent(struct expression
*expr
, struct smatch_state
*state
);
49 static const char *allocation_funcs
[] = {
56 static char *alloc_parent_str(struct symbol
*sym
)
60 if (!sym
|| !sym
->ident
)
63 snprintf(buf
, 255, "%s", sym
->ident
->name
);
65 return alloc_string(buf
);
68 static char *get_parent_from_expr(struct expression
*expr
, struct symbol
**sym
)
72 expr
= strip_expr(expr
);
74 name
= expr_to_str_sym(expr
, sym
);
76 if (!name
|| !*sym
|| !(*sym
)->ident
) {
80 return alloc_parent_str(*sym
);
83 static int is_local(struct expression
*expr
)
89 name
= expr_to_str_sym(expr
, &sym
);
92 if (sym
->ctype
.modifiers
& (MOD_NONLOCAL
| MOD_STATIC
| MOD_ADDRESSABLE
))
100 static int is_param(struct expression
*expr
)
107 name
= expr_to_str_sym(expr
, &sym
);
110 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, tmp
) {
115 } END_FOR_EACH_PTR(tmp
);
122 static void match_alloc(const char *fn
, struct expression
*expr
, void *unused
)
124 if (__in_fake_var_assign
)
126 if (!is_local(expr
->left
))
128 if (is_param(expr
->left
))
130 if (expr
->left
->type
!= EXPR_SYMBOL
)
132 set_state_expr(my_id
, expr
->left
, &allocated
);
135 static void match_condition(struct expression
*expr
)
139 expr
= strip_expr(expr
);
141 switch (expr
->type
) {
145 sm
= get_sm_state_expr(my_id
, expr
);
146 if (sm
&& slist_has_state(sm
->possible
, &allocated
))
147 set_true_false_states_expr(my_id
, expr
, NULL
, &ok
);
149 case EXPR_ASSIGNMENT
:
150 /* You have to deal with stuff like if (a = b = c) */
151 match_condition(expr
->left
);
158 static void set_parent(struct expression
*expr
, struct smatch_state
*state
)
163 expr
= strip_expr(expr
);
166 if (expr
->type
== EXPR_CONDITIONAL
||
167 expr
->type
== EXPR_SELECT
) {
168 set_parent(expr
->cond_true
, state
);
169 set_parent(expr
->cond_false
, state
);
173 name
= get_parent_from_expr(expr
, &sym
);
176 if (state
== &ok
&& !get_state(my_id
, name
, sym
))
178 set_state(my_id
, name
, sym
, state
);
183 static void match_function_call(struct expression
*expr
)
185 struct expression
*tmp
;
187 FOR_EACH_PTR(expr
->args
, tmp
) {
188 set_parent(tmp
, &ok
);
189 } END_FOR_EACH_PTR(tmp
);
192 static void warn_if_allocated(struct expression
*expr
)
198 if (get_implied_value(expr
, &sval
) && sval
.value
== 0)
201 sm
= get_sm_state_expr(my_id
, expr
);
204 if (!slist_has_state(sm
->possible
, &allocated
))
207 name
= expr_to_var(expr
);
208 sm_warning("overwrite may leak '%s'", name
);
211 /* silence further warnings */
212 set_state_expr(my_id
, expr
, &ok
);
215 static void match_assign(struct expression
*expr
)
217 struct expression
*right
;
221 while (right
->type
== EXPR_ASSIGNMENT
)
224 warn_if_allocated(expr
->left
);
225 set_parent(right
, &ok
);
228 static void check_for_allocated(void)
231 struct sm_state
*tmp
;
233 stree
= __get_cur_stree();
234 FOR_EACH_MY_SM(my_id
, stree
, tmp
) {
235 if (!slist_has_state(tmp
->possible
, &allocated
))
237 sm_warning("possible memory leak of '%s'", tmp
->name
);
238 } END_FOR_EACH_SM(tmp
);
241 static void match_return(struct expression
*ret_value
)
245 set_parent(ret_value
, &ok
);
246 check_for_allocated();
249 static void match_end_func(struct symbol
*sym
)
253 check_for_allocated();
256 void check_leaks(int id
)
262 for (i
= 0; i
< ARRAY_SIZE(allocation_funcs
); i
++)
263 add_function_assign_hook(allocation_funcs
[i
], &match_alloc
, NULL
);
265 add_hook(&match_condition
, CONDITION_HOOK
);
267 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
268 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
270 add_hook(&match_return
, RETURN_HOOK
);
271 add_hook(&match_end_func
, END_FUNC_HOOK
);