extra: don't overwrite non-null pointers because of a dereference
[smatch.git] / check_release_resource.c
blob1c7e2abf86ee862fbc1548d817aad8ef28eb140e
1 /*
2 * Copyright (C) 2010 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * I found a bug where someone released the wrong resource and wanted to
20 * prevent that from happening again.
24 #include "smatch.h"
26 static int my_id;
28 static struct tracker_list *resource_list;
30 static void match_request(const char *fn, struct expression *expr, void *_arg_no)
32 struct expression *arg_expr;
33 int arg_no = PTR_INT(_arg_no);
34 char *name;
35 struct symbol *sym;
37 arg_expr = get_argument_from_call_expr(expr->args, arg_no);
38 arg_expr = strip_expr(arg_expr);
40 name = expr_to_var_sym(arg_expr, &sym);
41 if (!name || !sym)
42 goto free;
43 add_tracker(&resource_list, my_id, name, sym);
44 free:
45 free_string(name);
48 static void match_release(const char *fn, struct expression *expr, void *_arg_no)
50 struct expression *arg_expr;
51 int arg_no = PTR_INT(_arg_no);
52 char *name;
53 struct symbol *sym;
55 arg_expr = get_argument_from_call_expr(expr->args, arg_no);
56 arg_expr = strip_expr(arg_expr);
58 if (!resource_list)
59 return;
61 name = expr_to_var_sym(arg_expr, &sym);
62 if (!name || !sym)
63 goto free;
64 if (in_tracker_list(resource_list, my_id, name, sym))
65 goto free;
66 sm_msg("warn: '%s' was not one of the resources you requested", name);
67 free:
68 free_string(name);
71 static void match_end_func(struct symbol *sym)
73 if (__inline_fn)
74 return;
75 free_trackers_and_list(&resource_list);
78 void check_release_resource(int id)
80 my_id = id;
82 if (option_project != PROJ_KERNEL)
83 return;
85 add_function_hook("request_resource", &match_request, (void *)1);
86 add_function_hook("release_resource", &match_release, (void *)0);
87 add_function_hook("request_mem_resource", &match_request, (void *)0);
88 add_function_hook("release_mem_resource", &match_release, (void *)0);
89 add_hook(&match_end_func, END_FUNC_HOOK);