1 /* 2 * Copyright (C) 2009 Dan Carpenter. 3 * 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. 8 * 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. 13 * 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 16 */ 17 18 #include "smatch.h" 19 20 static int my_id; 21 22 static int in_function(const char *fn) 23 { 24 char *cur_func = get_function(); 25 26 if (!cur_func) 27 return 0; 28 if (!strcmp(cur_func, fn)) 29 return 1; 30 return 0; 31 } 32 33 static void match_free(const char *fn, struct expression *expr, void *data) 34 { 35 struct expression *arg_expr; 36 char *name; 37 struct symbol *type; 38 39 arg_expr = get_argument_from_call_expr(expr->args, 0); 40 type = get_pointer_type(arg_expr); 41 if (!type || !type->ident) 42 return; 43 44 name = expr_to_str(arg_expr); 45 46 if (!strcmp("sk_buff", type->ident->name)) { 47 sm_error("use kfree_skb() here instead of kfree(%s)", name); 48 } else if (!strcmp("net_device", type->ident->name)) { 49 if (in_function("alloc_netdev")) 50 return; 51 if (in_function("alloc_netdev_mqs")) 52 return; 53 sm_error("use free_netdev() here instead of kfree(%s)", name); 54 } 55 56 free_string(name); 57 } 58 59 void check_type(int id) 60 { 61 my_id = id; 62 if (option_project == PROJ_KERNEL) 63 add_function_hook("kfree", &match_free, NULL); 64 } 65