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
19 * This is like check_deref_check.c except that it complains about code like:
24 * Of course, Smatch has complained about these for forever but the problem is
25 * the old scripts were too messy and complicated and generated too many false
28 * This check is supposed to be simpler because it only looks for one kind of
29 * null dereference bug instead of every kind. It also gets rid of the false
30 * positives caused by the checks that happen inside macros.
35 #include "smatch_slist.h"
36 #include "smatch_extra.h"
43 static void is_ok(struct sm_state
*sm
, struct expression
*mod_expr
)
45 set_state(my_id
, sm
->name
, sm
->sym
, &ok
);
48 static void check_dereference(struct expression
*expr
)
56 expr
= strip_expr(expr
);
58 sm
= get_sm_state_expr(my_id
, expr
);
61 if (is_ignored(my_id
, sm
->name
, sm
->sym
))
63 if (implied_not_equal(expr
, 0))
66 FOR_EACH_PTR(sm
->possible
, tmp
) {
67 if (tmp
->state
== &merged
)
69 if (tmp
->state
== &ok
)
71 if (tmp
->state
== &null
) {
72 sm_error("we previously assumed '%s' could be null (see line %d)",
73 tmp
->name
, tmp
->line
);
74 add_ignore(my_id
, sm
->name
, sm
->sym
);
77 } END_FOR_EACH_PTR(tmp
);
80 static void check_dereference_name_sym(char *name
, struct symbol
*sym
)
85 sm
= get_sm_state(my_id
, name
, sym
);
88 if (is_ignored(my_id
, sm
->name
, sm
->sym
))
90 if (implied_not_equal_name_sym(name
, sym
, 0))
93 FOR_EACH_PTR(sm
->possible
, tmp
) {
94 if (tmp
->state
== &merged
)
96 if (tmp
->state
== &ok
)
98 if (tmp
->state
== &null
) {
99 sm_error("we previously assumed '%s' could be null (see line %d)",
100 tmp
->name
, tmp
->line
);
101 add_ignore(my_id
, sm
->name
, sm
->sym
);
104 } END_FOR_EACH_PTR(tmp
);
107 static void match_dereferences(struct expression
*expr
)
109 if (expr
->type
!= EXPR_PREOP
)
111 check_dereference(expr
->unop
);
114 static void match_pointer_as_array(struct expression
*expr
)
118 check_dereference(get_array_base(expr
));
121 static void set_param_dereferenced(struct expression
*call
, struct expression
*arg
, char *key
, char *unused
)
126 name
= get_variable_from_key(arg
, key
, &sym
);
130 check_dereference_name_sym(name
, sym
);
135 static void match_condition(struct expression
*expr
)
137 struct smatch_state
*true_state
= NULL
;
140 name
= get_macro_name(expr
->pos
);
142 (strcmp(name
, "likely") != 0 && strcmp(name
, "unlikely") != 0))
145 if (!is_pointer(expr
))
148 if (expr
->type
== EXPR_ASSIGNMENT
) {
149 match_condition(expr
->right
);
150 match_condition(expr
->left
);
153 if (implied_not_equal(expr
, 0))
156 if (get_state_expr(my_id
, expr
))
159 set_true_false_states_expr(my_id
, expr
, true_state
, &null
);
162 void check_check_deref(int id
)
166 add_modification_hook(my_id
, &is_ok
);
167 add_hook(&match_dereferences
, DEREF_HOOK
);
168 add_hook(&match_pointer_as_array
, OP_HOOK
);
169 select_return_implies_hook(DEREFERENCE
, &set_param_dereferenced
);
170 add_hook(&match_condition
, CONDITION_HOOK
);