refcount: partial increments count as increments
[smatch.git] / check_allocation_funcs.c
blobf6bf40890e71d45694a8d55e775f8e84007ebe59
1 /*
2 * Copyright (C) 2009 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
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include "parse.h"
21 #include "smatch.h"
22 #include "smatch_slist.h"
24 static int my_id;
27 * Print a list of functions that return newly allocated memory.
30 static struct tracker_list *allocated;
32 static void match_allocation(struct expression *expr,
33 const char *name, struct symbol *sym,
34 struct allocation_info *info)
36 if (!sym)
37 return;
38 if (sym->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
39 return;
40 add_tracker(&allocated, my_id, name, sym);
43 static unsigned long returns_new_stuff;
44 static unsigned long returns_old_stuff;
46 static void match_return(struct expression *ret_value)
48 char *name;
49 struct symbol *sym;
50 sval_t tmp;
52 if (__inline_fn)
53 return;
54 if (get_value(ret_value, &tmp) && tmp.value == 0)
55 return;
56 returns_new_stuff = 1;
57 name = expr_to_var_sym(ret_value, &sym);
58 if (!name || !sym) {
59 returns_old_stuff = 1;
60 goto free;
62 if (!in_tracker_list(allocated, my_id, name, sym))
63 returns_old_stuff = 1;
64 free:
65 free_string(name);
68 static void match_end_func(struct symbol *sym)
70 if (__inline_fn)
71 return;
72 if (returns_new_stuff && !returns_old_stuff)
73 sm_info("allocation func");
74 free_trackers_and_list(&allocated);
77 void check_allocation_funcs(int id)
79 if (!option_info || option_project != PROJ_KERNEL)
80 return;
82 my_id = id;
84 add_function_data(&returns_old_stuff);
85 add_function_data(&returns_new_stuff);
87 add_allocation_hook(&match_allocation);
89 add_hook(&match_return, RETURN_HOOK);
90 add_hook(&match_end_func, AFTER_FUNC_HOOK);