err_ptr_deref: make err_ptr_min/max static
[smatch.git] / check_check_deref.c
blobc650bd4e64c7208fb7d8c10122e933598f0b1c1c
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
19 * This is like check_deref_check.c except that it complains about code like:
20 * if (a)
21 * a->foo = 42;
22 * a->bar = 7;
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
26 * positives.
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.
34 #include "smatch.h"
35 #include "smatch_slist.h"
36 #include "smatch_extra.h"
38 static int my_id;
40 STATE(null);
41 STATE(ok);
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)
50 struct sm_state *sm;
51 struct sm_state *tmp;
53 expr = strip_expr(expr);
55 sm = get_sm_state_expr(my_id, expr);
56 if (!sm)
57 return;
58 if (is_ignored(my_id, sm->name, sm->sym))
59 return;
60 if (implied_not_equal(expr, 0))
61 return;
63 FOR_EACH_PTR(sm->possible, tmp) {
64 if (tmp->state == &merged)
65 continue;
66 if (tmp->state == &ok)
67 continue;
68 if (tmp->state == &null) {
69 sm_msg("error: we previously assumed '%s' could be null (see line %d)",
70 tmp->name, tmp->line);
71 add_ignore(my_id, sm->name, sm->sym);
72 return;
74 } END_FOR_EACH_PTR(tmp);
77 static void check_dereference_name_sym(char *name, struct symbol *sym)
79 struct sm_state *sm;
80 struct sm_state *tmp;
82 sm = get_sm_state(my_id, name, sym);
83 if (!sm)
84 return;
85 if (is_ignored(my_id, sm->name, sm->sym))
86 return;
87 if (implied_not_equal_name_sym(name, sym, 0))
88 return;
90 FOR_EACH_PTR(sm->possible, tmp) {
91 if (tmp->state == &merged)
92 continue;
93 if (tmp->state == &ok)
94 continue;
95 if (tmp->state == &null) {
96 sm_msg("error: we previously assumed '%s' could be null (see line %d)",
97 tmp->name, tmp->line);
98 add_ignore(my_id, sm->name, sm->sym);
99 return;
101 } END_FOR_EACH_PTR(tmp);
104 static void match_dereferences(struct expression *expr)
106 if (expr->type != EXPR_PREOP)
107 return;
108 check_dereference(expr->unop);
111 static void match_pointer_as_array(struct expression *expr)
113 if (!is_array(expr))
114 return;
115 check_dereference(get_array_base(expr));
118 static void set_param_dereferenced(struct expression *arg, char *key, char *unused)
120 struct symbol *sym;
121 char *name;
123 name = get_variable_from_key(arg, key, &sym);
124 if (!name || !sym)
125 goto free;
127 check_dereference_name_sym(name, sym);
128 free:
129 free_string(name);
132 static void match_condition(struct expression *expr)
134 if (get_macro_name(expr->pos))
135 return;
137 if (!is_pointer(expr))
138 return;
140 if (expr->type == EXPR_ASSIGNMENT) {
141 match_condition(expr->right);
142 match_condition(expr->left);
144 set_true_false_states_expr(my_id, expr, &ok, &null);
147 void check_check_deref(int id)
149 my_id = id;
151 add_modification_hook(my_id, &is_ok);
152 add_hook(&match_dereferences, DEREF_HOOK);
153 add_hook(&match_pointer_as_array, OP_HOOK);
154 select_call_implies_hook(DEREFERENCE, &set_param_dereferenced);
155 add_hook(&match_condition, CONDITION_HOOK);