helper: taking the address is not a complicated variable
[smatch.git] / check_puts_argument.c
blob7e582ac76c6041c1f0efa7767298f8a3d8623157
1 /*
2 * sparse/check_puts_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(putted);
22 static struct symbol *this_func;
23 static struct tracker_list *putted_args = NULL;
25 static void match_function_def(struct symbol *sym)
27 this_func = sym;
30 static int parent_is_arg(struct symbol *sym)
32 struct symbol *arg;
34 FOR_EACH_PTR(this_func->ctype.base_type->arguments, arg) {
35 if (sym == arg)
36 return 1;
37 } END_FOR_EACH_PTR(arg);
38 return 0;
41 static void match_put(const char *fn, struct expression *expr, void *info)
43 struct expression *tmp;
44 struct symbol *sym;
45 char *name;
47 tmp = get_argument_from_call_expr(expr->args, 0);
48 tmp = strip_expr(tmp);
49 name = expr_to_var_sym(tmp, &sym);
50 free_string(name);
51 if (parent_is_arg(sym) && sym->ident)
52 set_state(my_id, sym->ident->name, sym, &putted);
55 static int return_count = 0;
56 static void match_return(struct expression *ret_value)
58 struct state_list *slist;
59 struct sm_state *tmp;
60 struct tracker *tracker;
62 if (__inline_fn)
63 return;
65 if (!return_count) {
66 slist = get_all_states(my_id);
67 FOR_EACH_PTR(slist, tmp) {
68 if (tmp->state == &putted)
69 add_tracker(&putted_args, my_id, tmp->name,
70 tmp->sym);
71 } END_FOR_EACH_PTR(tmp);
72 free_slist(&slist);
73 } else {
74 FOR_EACH_PTR(putted_args, tracker) {
75 tmp = get_sm_state(my_id, tracker->name, tracker->sym);
76 if (tmp && tmp->state != &putted)
77 del_tracker(&putted_args, my_id, tracker->name,
78 tracker->sym);
79 } END_FOR_EACH_PTR(tracker);
84 static void print_arg(struct symbol *sym)
86 struct symbol *arg;
87 int i = 0;
89 FOR_EACH_PTR(this_func->ctype.base_type->arguments, arg) {
90 if (sym == arg) {
91 sm_info("puts_arg %s %d", get_function(), i);
92 return;
94 i++;
95 } END_FOR_EACH_PTR(arg);
98 static void match_end_func(struct symbol *sym)
100 struct tracker *tracker;
102 if (__inline_fn)
103 return;
104 if (is_reachable())
105 match_return(NULL);
107 FOR_EACH_PTR(putted_args, tracker) {
108 print_arg(tracker->sym);
109 } END_FOR_EACH_PTR(tracker);
111 free_trackers_and_list(&putted_args);
112 return_count = 0;
115 void check_puts_argument(int id)
117 if (!option_info || option_project != PROJ_KERNEL)
118 return;
120 my_id = id;
121 add_hook(&match_function_def, FUNC_DEF_HOOK);
122 add_function_hook("kobject_put", &match_put, NULL);
123 add_function_hook("kref_put", &match_put, NULL);
124 add_hook(&match_return, RETURN_HOOK);
125 add_hook(&match_end_func, END_FUNC_HOOK);