__module_put_and_exit() doesn't return.
[smatch.git] / check_allocation_funcs.c
blob072eaf19f34960407c35fa47df9671d57230308a
1 /*
2 * sparse/check_allocation_funcs.c
4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include "parse.h"
13 #include "smatch.h"
14 #include "smatch_slist.h"
16 static int my_id;
19 * Print a list of functions that return newly allocated memory.
22 static struct tracker_list *allocated;
24 static const char *allocation_funcs[] = {
25 "malloc",
26 "kmalloc",
27 "kzalloc",
28 NULL,
31 static void match_allocation(const char *fn, struct expression *expr,
32 void *info)
34 char *left_name;
35 struct symbol *left_sym;
37 left_name = get_variable_from_expr(expr->left, &left_sym);
38 if (!left_name || !left_sym)
39 goto free;
40 if (left_sym->ctype.modifiers &
41 (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
42 goto free;
43 add_tracker(&allocated, left_name, my_id, left_sym);
44 free:
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)
52 char *name;
53 struct symbol *sym;
55 if (get_value(stmt->ret_value) == 0)
56 return;
57 returns_new_stuff = 1;
58 name = get_variable_from_expr(stmt->ret_value, &sym);
59 if (!name || !sym) {
60 returns_old_stuff = 1;
61 goto free;
63 if (!in_tracker_list(allocated, name, my_id, sym))
64 returns_old_stuff = 1;
65 free:
66 free_string(name);
69 static void match_end_func(struct symbol *sym)
71 if (returns_new_stuff && !returns_old_stuff)
72 smatch_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)
80 int i;
82 my_id = id;
83 add_hook(&match_return, RETURN_HOOK);
84 add_hook(&match_end_func, END_FUNC_HOOK);
85 for(i = 0; allocation_funcs[i]; i++) {
86 add_function_assign_hook(allocation_funcs[i],
87 &match_allocation, NULL);