2 * sparse/check_allocation_funcs.c
4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
14 #include "smatch_slist.h"
19 * Print a list of functions that return newly allocated memory.
22 static struct tracker_list
*allocated
;
24 static const char *allocation_funcs
[] = {
31 static void match_allocation(const char *fn
, struct expression
*expr
,
35 struct symbol
*left_sym
;
37 left_name
= get_variable_from_expr(expr
->left
, &left_sym
);
38 if (!left_name
|| !left_sym
)
40 if (left_sym
->ctype
.modifiers
&
41 (MOD_NONLOCAL
| MOD_STATIC
| MOD_ADDRESSABLE
))
43 add_tracker(&allocated
, my_id
, left_name
, left_sym
);
45 free_string(left_name
);
48 static int returns_new_stuff
= 0;
49 static int returns_old_stuff
= 0;
50 static void match_return(struct expression
*ret_value
)
56 if (get_value(ret_value
, &tmp
) && tmp
== 0)
58 returns_new_stuff
= 1;
59 name
= get_variable_from_expr(ret_value
, &sym
);
61 returns_old_stuff
= 1;
64 if (!in_tracker_list(allocated
, my_id
, name
, sym
))
65 returns_old_stuff
= 1;
70 static void match_end_func(struct symbol
*sym
)
72 if (returns_new_stuff
&& !returns_old_stuff
)
73 sm_info("allocation func");
74 free_trackers_and_list(&allocated
);
75 returns_new_stuff
= 0;
76 returns_old_stuff
= 0;
79 void check_allocation_funcs(int id
)
83 if (!option_info
|| option_project
!= PROJ_KERNEL
)
87 add_hook(&match_return
, RETURN_HOOK
);
88 add_hook(&match_end_func
, END_FUNC_HOOK
);
89 for(i
= 0; allocation_funcs
[i
]; i
++) {
90 add_function_assign_hook(allocation_funcs
[i
],
91 &match_allocation
, NULL
);