forked from error27/smatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_kmalloc_to_bugon.c
61 lines (54 loc) · 1.55 KB
/
check_kmalloc_to_bugon.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* Copyright (C) 2010 Dan Carpenter.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
*/
#include "smatch.h"
static int my_id;
static int is_kmalloc_call(struct expression *expr)
{
if (expr->type != EXPR_CALL)
return 0;
if (expr->fn->type != EXPR_SYMBOL)
return 0;
if (!strcmp(expr->fn->symbol_name->name, "kmalloc"))
return 1;
if (!strcmp(expr->fn->symbol_name->name, "kzalloc"))
return 1;
return 0;
}
static void match_condition(struct expression *expr)
{
char *macro;
struct expression *right;
char *name;
macro = get_macro_name(expr->pos);
if (!macro || strcmp(macro, "BUG_ON") != 0)
return;
right = get_assigned_expr(expr);
if (!right || !is_kmalloc_call(right))
return;
name = expr_to_var(expr);
sm_warning("bug on allocation failure '%s'", name);
free_string(name);
}
void check_kmalloc_to_bugon(int id)
{
if (option_project != PROJ_KERNEL)
return;
if (!option_spammy)
return;
my_id = id;
add_hook(&match_condition, CONDITION_HOOK);
}