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 statement
*stmt
)
55 if (get_value(stmt
->ret_value
) == 0)
57 returns_new_stuff
= 1;
58 name
= get_variable_from_expr(stmt
->ret_value
, &sym
);
60 returns_old_stuff
= 1;
63 if (!in_tracker_list(allocated
, my_id
, name
, sym
))
64 returns_old_stuff
= 1;
69 static void match_end_func(struct symbol
*sym
)
71 if (returns_new_stuff
&& !returns_old_stuff
)
72 sm_msg("info: allocation func");
73 free_trackers_and_list(&allocated
);
74 returns_new_stuff
= 0;
75 returns_old_stuff
= 0;
78 void check_allocation_funcs(int id
)
86 add_hook(&match_return
, RETURN_HOOK
);
87 add_hook(&match_end_func
, END_FUNC_HOOK
);
88 for(i
= 0; allocation_funcs
[i
]; i
++) {
89 add_function_assign_hook(allocation_funcs
[i
],
90 &match_allocation
, NULL
);