sval: update check_deref.c
[smatch.git] / check_type.c
blob828e3523d260763f99892ed60dea099f9eec3fa5
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_pointer_type(arg_expr);
33 if (!type || !type->ident)
34 return;
36 name = get_variable_from_expr_complex(arg_expr, NULL);
38 if (!strcmp("sk_buff", type->ident->name)) {
39 sm_msg("error: use kfree_skb() here instead of kfree(%s)", name);
40 } else if (!strcmp("net_device", type->ident->name)) {
41 if (in_function("alloc_netdev"))
42 return;
43 if (in_function("alloc_netdev_mqs"))
44 return;
45 sm_msg("error: use free_netdev() here instead of kfree(%s)", name);
48 free_string(name);
51 void check_type(int id)
53 my_id = id;
54 if (option_project == PROJ_KERNEL)
55 add_function_hook("kfree", &match_free, NULL);