function_hooks: fix locking false positives when you have no db
[smatch.git] / check_kmalloc_to_bugon.c
blob0e450c733459f5103d67e4f16a05d76055faeff9
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 extern int check_assigned_expr_id;
24 static int is_kmalloc_call(struct expression *expr)
26 if (expr->type != EXPR_CALL)
27 return 0;
28 if (expr->fn->type != EXPR_SYMBOL)
29 return 0;
30 if (!strcmp(expr->fn->symbol_name->name, "kmalloc"))
31 return 1;
32 if (!strcmp(expr->fn->symbol_name->name, "kzalloc"))
33 return 1;
34 return 0;
37 static void match_condition(struct expression *expr)
39 char *macro;
40 struct smatch_state *state;
41 struct expression *right;
42 char *name;
44 macro = get_macro_name(expr->pos);
45 if (!macro || strcmp(macro, "BUG_ON") != 0)
46 return;
47 state = get_state_expr(check_assigned_expr_id, expr);
48 if (!state || !state->data)
49 return;
50 right = (struct expression *)state->data;
51 if (!is_kmalloc_call(right))
52 return;
54 name = expr_to_var(expr);
55 sm_msg("warn: bug on allocation failure '%s'", name);
56 free_string(name);
59 void check_kmalloc_to_bugon(int id)
61 if (option_project != PROJ_KERNEL)
62 return;
63 if (!option_spammy)
64 return;
65 my_id = id;
66 add_hook(&match_condition, CONDITION_HOOK);