flow: fix segfault on parse error
[smatch.git] / check_allocation_funcs.c
blobcbafb007943b123b870751f15433c5c9e3baedaf
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 "kmalloc",
26 "kzalloc",
27 "kcalloc",
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, my_id, left_name, 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 expression *ret_value)
52 char *name;
53 struct symbol *sym;
54 long long tmp;
56 if (get_value(ret_value, &tmp) && tmp == 0)
57 return;
58 returns_new_stuff = 1;
59 name = get_variable_from_expr(ret_value, &sym);
60 if (!name || !sym) {
61 returns_old_stuff = 1;
62 goto free;
64 if (!in_tracker_list(allocated, my_id, name, sym))
65 returns_old_stuff = 1;
66 free:
67 free_string(name);
70 static void match_end_func(struct symbol *sym)
72 if (returns_new_stuff && !returns_old_stuff)
73 sm_info("allocation func");
74 free_trackers_and_list(&allocated);
75 returns_new_stuff = 0;
76 returns_old_stuff = 0;
79 void check_allocation_funcs(int id)
81 int i;
83 if (!option_info || option_project != PROJ_KERNEL)
84 return;
86 my_id = id;
87 add_hook(&match_return, RETURN_HOOK);
88 add_hook(&match_end_func, END_FUNC_HOOK);
89 for (i = 0; allocation_funcs[i]; i++) {
90 add_function_assign_hook(allocation_funcs[i],
91 &match_allocation, NULL);