kernel.return_fixes: add mipi_dsi_device_transfer(), timer_delete() and get_device()
[smatch.git] / check_kmalloc_to_bugon.c
blob18ebaa2104e0d4186aa87621247db519354f2752
1 /*
2 * Copyright (C) 2010 Dan Carpenter.
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.
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.
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
18 #include "smatch.h"
20 static int my_id;
22 static int is_kmalloc_call(struct expression *expr)
24 if (expr->type != EXPR_CALL)
25 return 0;
26 if (expr->fn->type != EXPR_SYMBOL)
27 return 0;
28 if (!strcmp(expr->fn->symbol_name->name, "kmalloc"))
29 return 1;
30 if (!strcmp(expr->fn->symbol_name->name, "kzalloc"))
31 return 1;
32 return 0;
35 static void match_condition(struct expression *expr)
37 char *macro;
38 struct expression *right;
39 char *name;
41 macro = get_macro_name(expr->pos);
42 if (!macro || strcmp(macro, "BUG_ON") != 0)
43 return;
44 right = get_assigned_expr(expr);
45 if (!right || !is_kmalloc_call(right))
46 return;
48 name = expr_to_var(expr);
49 sm_warning("bug on allocation failure '%s'", name);
50 free_string(name);
53 void check_kmalloc_to_bugon(int id)
55 if (option_project != PROJ_KERNEL)
56 return;
57 if (!option_spammy)
58 return;
59 my_id = id;
60 add_hook(&match_condition, CONDITION_HOOK);