-
Notifications
You must be signed in to change notification settings - Fork 5
/
check_deref_check.c
91 lines (72 loc) · 2.34 KB
/
check_deref_check.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
/*
* Copyright (C) 2009 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"
#include "smatch_slist.h"
#include "smatch_extra.h"
static int my_id;
STATE(derefed);
static void deref_hook(struct expression *expr)
{
if (implied_not_equal(expr, 0))
return;
if (is_impossible_path())
return;
set_state_expr(my_id, expr, &derefed);
}
static void match_condition(struct expression *expr)
{
struct sm_state *sm;
char *name;
if (__in_pre_condition)
return;
name = get_macro_name(expr->pos);
if (name &&
(strcmp(name, "likely") != 0 && strcmp(name, "unlikely") != 0))
return;
if (!is_pointer(expr))
return;
sm = get_sm_state_expr(my_id, expr);
if (!sm || sm->state != &derefed)
return;
sm_warning("variable dereferenced before check '%s' (see line %d)", sm->name, sm->line);
set_state_expr(my_id, expr, &undefined);
}
static void match_err_check(struct expression *expr, const char *name, struct symbol *sym, void *data)
{
struct sm_state *sm;
char *macro;
if (__in_pre_condition)
return;
sm = get_sm_state(my_id, name, sym);
if (!sm || sm->state != &derefed)
return;
macro = get_macro_name(expr->pos);
if (macro && strcmp(macro, "gvt_vgpu_err") == 0)
return;
sm_warning("variable dereferenced before IS_ERR check '%s' (see line %d)", sm->name, sm->line);
set_state_expr(my_id, expr, &undefined);
}
void check_deref_check(int id)
{
my_id = id;
add_dereference_hook(deref_hook);
add_modification_hook(my_id, &set_undefined);
add_hook(&match_condition, CONDITION_HOOK);
add_function_param_key_hook("IS_ERR", &match_err_check, 0, "$", NULL);
add_function_param_key_hook("PTR_ERR_OR_ZERO", &match_err_check, 0, "$", NULL);
add_function_param_key_hook("IS_ERR_OR_NULL", &match_err_check, 0, "$", NULL);
}