estate: introduce estate_ranges() helper function
[smatch.git] / check_type.c
blobfec8161ea18f3b95ec4c436876b6eca912021ec0
1 /*
2 * sparse/check_type.c
4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include "smatch.h"
12 static int my_id;
14 static int in_function(const char *fn)
16 char *cur_func = get_function();
18 if (!cur_func)
19 return 0;
20 if (!strcmp(cur_func, fn))
21 return 1;
22 return 0;
25 static void match_free(const char *fn, struct expression *expr, void *data)
27 struct expression *arg_expr;
28 char *name;
29 struct symbol *type;
31 arg_expr = get_argument_from_call_expr(expr->args, 0);
32 type = get_type(arg_expr);
34 if (!type || (type->type != SYM_PTR && type->type != SYM_ARRAY))
35 return;
36 type = get_base_type(type);
38 if (!type || !type->ident)
39 return;
41 name = get_variable_from_expr_complex(arg_expr, NULL);
43 if (!strcmp("sk_buff", type->ident->name)) {
44 sm_msg("error: use kfree_skb() here instead of kfree(%s)", name);
45 } else if (!strcmp("net_device", type->ident->name)) {
46 if (in_function("alloc_netdev"))
47 return;
48 if (in_function("alloc_netdev_mqs"))
49 return;
50 sm_msg("error: use free_netdev() here instead of kfree(%s)", name);
53 free_string(name);
56 void check_type(int id)
58 my_id = id;
59 if (option_project == PROJ_KERNEL)
60 add_function_hook("kfree", &match_free, NULL);