*new* stored_conditions: make a list of known true/false conditions
[smatch.git] / check_frees_argument.c
blob93cc286016f4f2fa6e0d8e62c8545c69580a0469
1 /*
2 * sparse/check_frees_argument.c
4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 /*
11 * This script is for finding functions like hcd_buffer_free() which free
12 * their arguments. After running it, add those functions to check_memory.c
15 #include "smatch.h"
16 #include "smatch_slist.h"
18 static int my_id;
20 STATE(freed);
22 static struct symbol *this_func;
23 static struct tracker_list *freed_args = NULL;
25 static void match_function_def(struct symbol *sym)
27 this_func = sym;
30 static int is_arg(char *name, struct symbol *sym)
32 struct symbol *arg;
33 const char *arg_name;
35 FOR_EACH_PTR(this_func->ctype.base_type->arguments, arg) {
36 arg_name = (arg->ident?arg->ident->name:"-");
37 if (sym == arg && !strcmp(name, arg_name))
38 return 1;
39 } END_FOR_EACH_PTR(arg);
40 return 0;
43 static void match_kfree(const char *fn, struct expression *expr, void *info)
45 struct expression *tmp;
46 struct symbol *sym;
47 char *name;
49 tmp = get_argument_from_call_expr(expr->args, 0);
50 tmp = strip_expr(tmp);
51 name = expr_to_var_sym(tmp, &sym);
52 if (is_arg(name, sym)) {
53 set_state(my_id, name, sym, &freed);
55 free_string(name);
58 static int return_count = 0;
59 static void match_return(struct expression *ret_value)
61 struct state_list *slist;
62 struct sm_state *tmp;
63 struct tracker *tracker;
65 if (__inline_fn)
66 return;
68 if (!return_count) {
69 slist = get_all_states(my_id);
70 FOR_EACH_PTR(slist, tmp) {
71 if (tmp->state == &freed)
72 add_tracker(&freed_args, my_id, tmp->name,
73 tmp->sym);
74 } END_FOR_EACH_PTR(tmp);
75 free_slist(&slist);
76 } else {
77 FOR_EACH_PTR(freed_args, tracker) {
78 tmp = get_sm_state(my_id, tracker->name, tracker->sym);
79 if (tmp && tmp->state != &freed)
80 del_tracker(&freed_args, my_id, tracker->name,
81 tracker->sym);
82 } END_FOR_EACH_PTR(tracker);
87 static void print_arg(struct symbol *sym)
89 struct symbol *arg;
90 int i = 0;
92 FOR_EACH_PTR(this_func->ctype.base_type->arguments, arg) {
93 if (sym == arg) {
94 sm_info("free_arg %s %d", get_function(), i);
95 return;
97 i++;
98 } END_FOR_EACH_PTR(arg);
101 static void match_end_func(struct symbol *sym)
103 struct tracker *tracker;
105 if (__inline_fn)
106 return;
107 if (is_reachable())
108 match_return(NULL);
110 FOR_EACH_PTR(freed_args, tracker) {
111 print_arg(tracker->sym);
112 } END_FOR_EACH_PTR(tracker);
114 free_trackers_and_list(&freed_args);
115 return_count = 0;
118 void check_frees_argument(int id)
120 if (!option_info)
121 return;
123 my_id = id;
124 add_hook(&match_function_def, FUNC_DEF_HOOK);
125 if (option_project == PROJ_KERNEL)
126 add_function_hook("kfree", &match_kfree, NULL);
127 else
128 add_function_hook("free", &match_kfree, NULL);
129 add_hook(&match_return, RETURN_HOOK);
130 add_hook(&match_end_func, END_FUNC_HOOK);