-
Notifications
You must be signed in to change notification settings - Fork 14
/
check_expects_err_ptr.c
94 lines (79 loc) · 2.18 KB
/
check_expects_err_ptr.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* 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 struct symbol *func_sym;
STATE(argument);
STATE(ok);
static void set_ok(struct sm_state *sm, struct expression *mod_expr)
{
if (sm->state != &ok)
set_state(my_id, sm->name, sm->sym, &ok);
}
static void match_function_def(struct symbol *sym)
{
struct symbol *arg;
func_sym = sym;
FOR_EACH_PTR(func_sym->ctype.base_type->arguments, arg) {
if (!arg->ident) {
continue;
}
set_state(my_id, arg->ident->name, arg, &argument);
} END_FOR_EACH_PTR(arg);
}
static int get_arg_num(struct expression *expr)
{
struct smatch_state *state;
struct symbol *arg;
struct symbol *this_arg;
int i;
expr = strip_expr(expr);
if (expr->type != EXPR_SYMBOL)
return -1;
this_arg = expr->symbol;
state = get_state_expr(my_id, expr);
if (!state || state != &argument)
return -1;
i = 0;
FOR_EACH_PTR(func_sym->ctype.base_type->arguments, arg) {
if (arg == this_arg)
return i;
i++;
} END_FOR_EACH_PTR(arg);
return -1;
}
static void match_is_err(const char *fn, struct expression *expr, void *unused)
{
struct expression *arg;
int arg_num;
arg = get_argument_from_call_expr(expr->args, 0);
arg_num = get_arg_num(arg);
if (arg_num < 0)
return;
sm_msg("info: expects ERR_PTR %d", arg_num);
}
void check_expects_err_ptr(int id)
{
if (option_project != PROJ_KERNEL)
return;
if (!option_info)
return;
my_id = id;
add_hook(&match_function_def, FUNC_DEF_HOOK);
add_modification_hook(my_id, &set_ok);
add_function_hook("IS_ERR", &match_is_err, NULL);
}