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
= get_variable_from_expr(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
= get_variable_from_expr(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
)
65 free_trackers_and_list(&resource_list
);
68 void check_release_resource(int id
)
72 if (option_project
!= PROJ_KERNEL
)
75 add_function_hook("request_resource", &match_request
, (void *)1);
76 add_function_hook("release_resource", &match_release
, (void *)0);
77 add_function_hook("request_mem_resource", &match_request
, (void *)0);
78 add_function_hook("release_mem_resource", &match_release
, (void *)0);
79 add_hook(&match_end_func
, END_FUNC_HOOK
);