double_checking: fix compile problem
[smatch.git] / check_deref.c
blob25d52be0f084893502e2a3b445a5527f0d149ea5
1 /*
2 * sparse/check_deref.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
11 * There was a previous null dereference test but it was too confusing and
12 * difficult to debug. This test is much simpler in its goals and scope.
14 * This test only complains about:
15 * 1) dereferencing uninitialized variables
16 * 2) dereferencing variables which were assigned as null.
17 * 3) dereferencing variables which were assigned a function the returns
18 * null.
20 * If we dereference something then we complain if any of those three
21 * are possible.
25 #include "smatch.h"
26 #include "smatch_slist.h"
27 #include "smatch_extra.h"
29 static int my_id;
31 #define __GFP_NOFAIL 0x800
33 STATE(null);
34 STATE(ok);
35 STATE(uninitialized);
37 static struct smatch_state *alloc_my_state(const char *name)
39 struct smatch_state *state;
41 state = __alloc_smatch_state(0);
42 state->name = name;
43 return state;
46 static struct smatch_state *unmatched_state(struct sm_state *sm)
48 return &ok;
51 static void is_ok(struct sm_state *sm, struct expression *mod_expr)
53 set_state(my_id, sm->name, sm->sym, &ok);
56 static void check_dereference(struct expression *expr)
58 struct sm_state *sm;
59 struct sm_state *tmp;
61 expr = strip_expr(expr);
62 sm = get_sm_state_expr(my_id, expr);
63 if (!sm)
64 return;
65 if (is_ignored(my_id, sm->name, sm->sym))
66 return;
67 if (implied_not_equal(expr, 0))
68 return;
70 FOR_EACH_PTR(sm->possible, tmp) {
71 if (tmp->state == &merged)
72 continue;
73 if (tmp->state == &ok)
74 continue;
75 add_ignore(my_id, sm->name, sm->sym);
76 if (tmp->state == &null) {
77 if (option_spammy)
78 sm_msg("error: potential NULL dereference '%s'.", tmp->name);
79 return;
81 if (tmp->state == &uninitialized) {
82 if (option_spammy)
83 sm_msg("error: potentially dereferencing uninitialized '%s'.", tmp->name);
84 return;
86 sm_msg("error: potential null dereference '%s'. (%s returns null)",
87 tmp->name, tmp->state->name);
88 return;
89 } END_FOR_EACH_PTR(tmp);
92 static void match_dereferences(struct expression *expr)
94 if (expr->type != EXPR_PREOP)
95 return;
96 check_dereference(expr->unop);
99 static void match_pointer_as_array(struct expression *expr)
101 if (!is_array(expr))
102 return;
103 check_dereference(expr->unop->left);
106 static void set_param_dereferenced(struct expression *arg, char *unused)
108 check_dereference(arg);
111 static void match_declarations(struct symbol *sym)
113 const char *name;
115 if ((get_base_type(sym))->type == SYM_ARRAY)
116 return;
118 if (!sym->ident)
119 return;
120 name = sym->ident->name;
121 if (!sym->initializer) {
122 set_state(my_id, name, sym, &uninitialized);
123 scoped_state(my_id, name, sym);
127 static void match_assign(struct expression *expr)
129 struct statement *stmt;
131 if (!is_zero(expr->right))
132 return;
134 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
135 if (stmt->type == STMT_DECLARATION)
136 return;
137 break;
138 } END_FOR_EACH_PTR_REVERSE(stmt);
140 set_state_expr(my_id, expr->left, &null);
143 static void match_condition(struct expression *expr)
145 if (expr->type == EXPR_ASSIGNMENT) {
146 match_condition(expr->right);
147 match_condition(expr->left);
149 if (!get_state_expr(my_id, expr))
150 return;
151 set_true_false_states_expr(my_id, expr, &ok, NULL);
154 static int called_with_no_fail(struct expression *call, int param)
156 struct expression *arg;
157 sval_t sval;
159 if (param == -1)
160 return 0;
161 call = strip_expr(call);
162 if (call->type != EXPR_CALL)
163 return 0;
164 arg = get_argument_from_call_expr(call->args, param);
165 if (get_value(arg, &sval) && (sval.uvalue & __GFP_NOFAIL))
166 return 1;
167 return 0;
170 static void match_assign_returns_null(const char *fn, struct expression *expr, void *_gfp)
172 struct smatch_state *state;
173 int gfp_param = PTR_INT(_gfp);
175 if (called_with_no_fail(expr->right, gfp_param))
176 return;
177 state = alloc_my_state(fn);
178 set_state_expr(my_id, expr->left, state);
181 static void register_allocation_funcs(void)
183 struct token *token;
184 const char *func;
185 int arg;
187 token = get_tokens_file("kernel.allocation_funcs_gfp");
188 if (!token)
189 return;
190 if (token_type(token) != TOKEN_STREAMBEGIN)
191 return;
192 token = token->next;
193 while (token_type(token) != TOKEN_STREAMEND) {
194 if (token_type(token) != TOKEN_IDENT)
195 return;
196 func = show_ident(token->ident);
197 token = token->next;
198 if (token_type(token) == TOKEN_IDENT)
199 arg = -1;
200 else if (token_type(token) == TOKEN_NUMBER)
201 arg = atoi(token->number);
202 else
203 return;
204 add_function_assign_hook(func, &match_assign_returns_null, INT_PTR(arg));
205 token = token->next;
207 clear_token_alloc();
210 void check_deref(int id)
212 my_id = id;
214 add_unmatched_state_hook(my_id, &unmatched_state);
215 add_modification_hook(my_id, &is_ok);
216 add_hook(&match_dereferences, DEREF_HOOK);
217 add_hook(&match_pointer_as_array, OP_HOOK);
218 select_call_implies_hook(DEREFERENCE, &set_param_dereferenced);
219 add_hook(&match_condition, CONDITION_HOOK);
220 add_hook(&match_declarations, DECLARATION_HOOK);
221 add_hook(&match_assign, ASSIGNMENT_HOOK);
222 if (option_project == PROJ_KERNEL)
223 register_allocation_funcs();