Print this page
11972 resync smatch
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/smatch/src/check_check_deref.c
+++ new/usr/src/tools/smatch/src/check_check_deref.c
1 1 /*
2 2 * Copyright (C) 2010 Dan Carpenter.
3 3 *
4 4 * This program is free software; you can redistribute it and/or
5 5 * modify it under the terms of the GNU General Public License
6 6 * as published by the Free Software Foundation; either version 2
7 7 * of the License, or (at your option) any later version.
8 8 *
9 9 * This program is distributed in the hope that it will be useful,
10 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 * GNU General Public License for more details.
13 13 *
14 14 * You should have received a copy of the GNU General Public License
15 15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16 16 */
17 17
18 18 /*
19 19 * This is like check_deref_check.c except that it complains about code like:
20 20 * if (a)
21 21 * a->foo = 42;
22 22 * a->bar = 7;
23 23 *
24 24 * Of course, Smatch has complained about these for forever but the problem is
25 25 * the old scripts were too messy and complicated and generated too many false
26 26 * positives.
27 27 *
28 28 * This check is supposed to be simpler because it only looks for one kind of
29 29 * null dereference bug instead of every kind. It also gets rid of the false
30 30 * positives caused by the checks that happen inside macros.
31 31 *
32 32 */
33 33
34 34 #include "smatch.h"
35 35 #include "smatch_slist.h"
36 36 #include "smatch_extra.h"
37 37
38 38 static int my_id;
39 39
40 40 STATE(null);
41 41 STATE(ok);
42 42
43 43 static void is_ok(struct sm_state *sm, struct expression *mod_expr)
44 44 {
45 45 set_state(my_id, sm->name, sm->sym, &ok);
46 46 }
47 47
48 48 static void check_dereference(struct expression *expr)
49 49 {
50 50 struct sm_state *sm;
51 51 struct sm_state *tmp;
52 52
53 53 if (__in_fake_assign)
54 54 return;
55 55
56 56 expr = strip_expr(expr);
57 57
58 58 sm = get_sm_state_expr(my_id, expr);
59 59 if (!sm)
60 60 return;
61 61 if (is_ignored(my_id, sm->name, sm->sym))
62 62 return;
63 63 if (implied_not_equal(expr, 0))
64 64 return;
65 65
66 66 FOR_EACH_PTR(sm->possible, tmp) {
67 67 if (tmp->state == &merged)
68 68 continue;
69 69 if (tmp->state == &ok)
70 70 continue;
71 71 if (tmp->state == &null) {
72 72 sm_error("we previously assumed '%s' could be null (see line %d)",
73 73 tmp->name, tmp->line);
74 74 add_ignore(my_id, sm->name, sm->sym);
75 75 return;
76 76 }
77 77 } END_FOR_EACH_PTR(tmp);
78 78 }
79 79
80 80 static void check_dereference_name_sym(char *name, struct symbol *sym)
81 81 {
82 82 struct sm_state *sm;
83 83 struct sm_state *tmp;
84 84
85 85 sm = get_sm_state(my_id, name, sym);
86 86 if (!sm)
87 87 return;
88 88 if (is_ignored(my_id, sm->name, sm->sym))
89 89 return;
90 90 if (implied_not_equal_name_sym(name, sym, 0))
91 91 return;
92 92
93 93 FOR_EACH_PTR(sm->possible, tmp) {
94 94 if (tmp->state == &merged)
95 95 continue;
96 96 if (tmp->state == &ok)
97 97 continue;
98 98 if (tmp->state == &null) {
99 99 sm_error("we previously assumed '%s' could be null (see line %d)",
100 100 tmp->name, tmp->line);
101 101 add_ignore(my_id, sm->name, sm->sym);
102 102 return;
103 103 }
104 104 } END_FOR_EACH_PTR(tmp);
105 105 }
106 106
107 107 static void match_dereferences(struct expression *expr)
108 108 {
109 109 if (expr->type != EXPR_PREOP)
110 110 return;
111 111 check_dereference(expr->unop);
112 112 }
113 113
114 114 static void match_pointer_as_array(struct expression *expr)
115 115 {
116 116 if (!is_array(expr))
117 117 return;
118 118 check_dereference(get_array_base(expr));
119 119 }
120 120
121 121 static void set_param_dereferenced(struct expression *call, struct expression *arg, char *key, char *unused)
122 122 {
123 123 struct symbol *sym;
124 124 char *name;
125 125
126 126 name = get_variable_from_key(arg, key, &sym);
127 127 if (!name || !sym)
↓ open down ↓ |
127 lines elided |
↑ open up ↑ |
128 128 goto free;
129 129
130 130 check_dereference_name_sym(name, sym);
131 131 free:
132 132 free_string(name);
133 133 }
134 134
135 135 static void match_condition(struct expression *expr)
136 136 {
137 137 struct smatch_state *true_state = NULL;
138 + char *name;
138 139
139 - if (get_macro_name(expr->pos))
140 + name = get_macro_name(expr->pos);
141 + if (name &&
142 + (strcmp(name, "likely") != 0 && strcmp(name, "unlikely") != 0))
140 143 return;
141 144
142 145 if (!is_pointer(expr))
143 146 return;
144 147
145 148 if (expr->type == EXPR_ASSIGNMENT) {
146 149 match_condition(expr->right);
147 150 match_condition(expr->left);
148 151 }
149 152
150 153 if (implied_not_equal(expr, 0))
151 154 return;
152 155
153 156 if (get_state_expr(my_id, expr))
154 157 true_state = &ok;
155 158
156 159 set_true_false_states_expr(my_id, expr, true_state, &null);
157 160 }
158 161
159 162 void check_check_deref(int id)
160 163 {
161 164 my_id = id;
162 165
163 166 add_modification_hook(my_id, &is_ok);
164 167 add_hook(&match_dereferences, DEREF_HOOK);
165 168 add_hook(&match_pointer_as_array, OP_HOOK);
166 169 select_return_implies_hook(DEREFERENCE, &set_param_dereferenced);
167 170 add_hook(&match_condition, CONDITION_HOOK);
168 171 }
↓ open down ↓ |
19 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX