2 * smatch/check_release_resource.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
11 * I found a bug where someone released the wrong resource and wanted to
12 * prevent that from happening again.
20 static struct tracker_list
*resource_list
;
22 static void match_request(const char *fn
, struct expression
*expr
, void *_arg_no
)
24 struct expression
*arg_expr
;
25 int arg_no
= PTR_INT(_arg_no
);
29 arg_expr
= get_argument_from_call_expr(expr
->args
, arg_no
);
30 arg_expr
= strip_expr(arg_expr
);
32 name
= expr_to_var_sym(arg_expr
, &sym
);
35 add_tracker(&resource_list
, my_id
, name
, sym
);
40 static void match_release(const char *fn
, struct expression
*expr
, void *_arg_no
)
42 struct expression
*arg_expr
;
43 int arg_no
= PTR_INT(_arg_no
);
47 arg_expr
= get_argument_from_call_expr(expr
->args
, arg_no
);
48 arg_expr
= strip_expr(arg_expr
);
53 name
= expr_to_var_sym(arg_expr
, &sym
);
56 if (in_tracker_list(resource_list
, my_id
, name
, sym
))
58 sm_msg("warn: '%s' was not one of the resources you requested", name
);
63 static void match_end_func(struct symbol
*sym
)
67 free_trackers_and_list(&resource_list
);
70 void check_release_resource(int id
)
74 if (option_project
!= PROJ_KERNEL
)
77 add_function_hook("request_resource", &match_request
, (void *)1);
78 add_function_hook("release_resource", &match_release
, (void *)0);
79 add_function_hook("request_mem_resource", &match_request
, (void *)0);
80 add_function_hook("release_mem_resource", &match_release
, (void *)0);
81 add_hook(&match_end_func
, END_FUNC_HOOK
);