2 * sparse/check_deference.c
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
12 #include "smatch_slist.h"
15 * TODO: The return_null list of functions should be determined automatically
17 const char *return_null
[] = {
26 ALLOCATOR(func_n_param
, "func parameters");
27 DECLARE_PTR_LIST(param_list
, struct func_n_param
);
29 static struct param_list
*funcs
;
30 static struct param_list
*do_not_call
;
31 static struct param_list
*calls
;
38 STATE(assumed_nonnull
);
43 static struct symbol
*func_sym
;
46 * merge_func() is to handle assumed_nonnull.
48 * Assumed_nonnull is a funny state. It's almost the same as
49 * no state. Maybe it would be better to delete it completely...
51 static struct smatch_state
*merge_func(const char *name
, struct symbol
*sym
,
52 struct smatch_state
*s1
,
53 struct smatch_state
*s2
)
55 if (s1
== &ignore
|| s2
== &ignore
)
57 if (s1
== NULL
&& s2
== &assumed_nonnull
)
58 return &assumed_nonnull
;
65 static int is_maybe_null(const char *name
, struct symbol
*sym
)
67 struct state_list
*slist
;
71 slist
= get_possible_states(name
, my_id
, sym
);
72 FOR_EACH_PTR(slist
, tmp
) {
73 if (tmp
->state
== &ignore
)
75 if (tmp
->state
== &isnull
) {
76 SM_DEBUG("is_maybe_null() says %s &isnull\n", name
);
79 if (tmp
->state
== &undefined
) {
80 SM_DEBUG("is_maybe_null() says %s &undefined\n", name
);
83 if (tmp
->state
== &arg_null
) {
84 SM_DEBUG("is_maybe_null() says %s &arg_null\n", name
);
87 } END_FOR_EACH_PTR(tmp
);
91 static int is_argument(char *name
, struct symbol
*sym
)
93 struct state_list
*slist
;
97 slist
= get_possible_states(name
, my_id
, sym
);
98 FOR_EACH_PTR(slist
, tmp
) {
99 if (tmp
->state
!= &argument
&& tmp
->state
!= &arg_null
&&
100 tmp
->state
!= &arg_nonnull
&& tmp
->state
!= &merged
)
102 if (tmp
->state
== &argument
|| tmp
->state
== &arg_null
)
104 } END_FOR_EACH_PTR(tmp
);
106 /* We really only care about arguments if they are null */
110 static struct func_n_param
*alloc_func_n_param(struct symbol
*func
, int param
,
113 struct func_n_param
*tmp
= __alloc_func_n_param(0);
121 static int get_arg_num(struct symbol
*sym
)
126 FOR_EACH_PTR(func_sym
->ctype
.base_type
->arguments
, arg
) {
131 } END_FOR_EACH_PTR(arg
);
135 static void add_do_not_call(struct symbol
*sym
, int line
)
137 struct func_n_param
*tmp
;
138 int num
= get_arg_num(sym
);
140 FOR_EACH_PTR(do_not_call
, tmp
) {
141 if (tmp
->func
== func_sym
&& tmp
->param
== num
)
143 } END_FOR_EACH_PTR(tmp
);
144 tmp
= alloc_func_n_param(func_sym
, num
, line
);
145 add_ptr_list(&do_not_call
, tmp
);
148 static void add_param(struct param_list
**list
, struct symbol
*func
, int param
,
151 struct func_n_param
*tmp
;
153 tmp
= alloc_func_n_param(func
, param
, line
);
154 add_ptr_list(list
, tmp
);
157 static void match_function_def(struct symbol
*sym
)
162 add_param(&funcs
, sym
, 0, 0);
163 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
167 set_state(arg
->ident
->name
, my_id
, arg
, &argument
);
168 } END_FOR_EACH_PTR(arg
);
171 static char *get_function_call(struct expression
*expr
)
173 if (expr
->type
!= EXPR_CALL
)
175 if (expr
->fn
->type
== EXPR_SYMBOL
&& expr
->fn
->symbol
)
176 return expr
->fn
->symbol
->ident
->name
;
180 static void match_function_call_after(struct expression
*expr
)
182 struct expression
*tmp
;
185 struct symbol
*func
= NULL
;
188 if (expr
->fn
->type
== EXPR_SYMBOL
)
189 func
= expr
->fn
->symbol
;
192 FOR_EACH_PTR(expr
->args
, tmp
) {
193 tmp
= strip_expr(tmp
);
194 if (tmp
->op
== '&') {
195 name
= get_variable_from_expr(tmp
->unop
, &sym
);
197 set_state(name
, my_id
, sym
, &assumed_nonnull
);
200 name
= get_variable_from_expr(tmp
, &sym
);
201 if (name
&& is_maybe_null(name
, sym
))
202 add_param(&calls
, func
, i
, get_lineno());
208 } END_FOR_EACH_PTR(tmp
);
211 static int check_null_returns(const char *name
, struct symbol
*sym
,
212 struct expression
*right
)
217 func_name
= get_function_call(right
);
221 for(i
= 0; i
< sizeof(*return_null
)/sizeof(return_null
[0]); i
++) {
222 if (!strcmp(func_name
,return_null
[i
])) {
223 set_state(name
, my_id
, sym
, &undefined
);
230 static int assign_seen
;
231 static void match_assign(struct expression
*expr
)
233 struct expression
*left
, *right
;
241 left
= strip_expr(expr
->left
);
242 name
= get_variable_from_expr(left
, &sym
);
245 right
= strip_expr(expr
->right
);
246 if (is_zero(right
)) {
247 set_state(name
, my_id
, sym
, &isnull
);
250 if (check_null_returns(name
, sym
, right
))
253 /* by default we assume it's assigned something nonnull */
254 set_state(name
, my_id
, sym
, &assumed_nonnull
);
258 * set_new_true_false_paths() is used in the following conditions
259 * if (a) { ... if (a) { ... } }
260 * The problem is that after the second blog a is set to undefined
261 * even though the second condition is meaning less. (The second test
262 * could be a macro for example).
264 * Also, stuff passed to the condition hook is processed behind the scenes
265 * a bit. Instead of a condition like (foo == 0) which is true when foo is
266 * null, we get just (foo) and the true and false states are adjusted later.
267 * Basically the important thing to remember, is that for us, true is always
268 * non null and false is always null.
270 static void set_new_true_false_paths(const char *name
, struct symbol
*sym
)
272 struct smatch_state
*tmp
;
274 tmp
= get_state(name
, my_id
, sym
);
276 SM_DEBUG("set_new_stuff called at for %s on line %d value='%s'\n",
277 name
, get_lineno(), show_state(tmp
));
279 if (tmp
== &argument
) {
280 set_true_false_states(name
, my_id
, sym
, &arg_nonnull
, &arg_null
);
284 if (!tmp
|| is_maybe_null(name
, sym
)) {
285 set_true_false_states(name
, my_id
, sym
, &nonnull
, &isnull
);
291 static void match_condition(struct expression
*expr
)
296 expr
= strip_expr(expr
);
301 name
= get_variable_from_expr(expr
, &sym
);
304 set_new_true_false_paths(name
, sym
);
306 case EXPR_ASSIGNMENT
:
309 * There is a kernel macro that does
310 * for ( ... ; ... || x = NULL ; ) ...
312 if (is_zero(expr
->right
)) {
313 name
= get_variable_from_expr(expr
->left
, &sym
);
316 set_true_false_states(name
, my_id
, sym
, NULL
, &isnull
);
319 /* You have to deal with stuff like if (a = b = c) */
320 match_condition(expr
->right
);
321 match_condition(expr
->left
);
328 static void match_declarations(struct symbol
*sym
)
332 if ((get_base_type(sym
))->type
== SYM_ARRAY
) {
336 name
= sym
->ident
->name
;
338 if (sym
->initializer
) {
339 if (is_zero(sym
->initializer
)) {
340 set_state(name
, my_id
, sym
, &isnull
);
343 if (check_null_returns(name
, sym
, strip_expr(sym
->initializer
)))
345 set_state(name
, my_id
, sym
, &assumed_nonnull
);
347 set_state(name
, my_id
, sym
, &undefined
);
351 static void match_dereferences(struct expression
*expr
)
354 struct symbol
*sym
= NULL
;
356 if (strcmp(show_special(expr
->deref
->op
), "*"))
359 deref
= get_variable_from_expr(expr
->deref
->unop
, &sym
);
363 if (is_argument(deref
, sym
)) {
364 add_do_not_call(sym
, get_lineno());
365 set_state(deref
, my_id
, sym
, &assumed_nonnull
);
366 } else if (is_maybe_null(deref
, sym
)) {
367 smatch_msg("Dereferencing Undefined: '%s'", deref
);
368 set_state(deref
, my_id
, sym
, &ignore
);
374 static void end_file_processing()
376 struct func_n_param
*param1
, *param2
;
378 // if a function is not static print it out...
379 FOR_EACH_PTR(do_not_call
, param1
) {
380 if (!(param1
->func
->ctype
.modifiers
& MOD_STATIC
))
381 printf("%s +%d unchecked param %s %d\n",
382 get_filename(), param1
->line
,
383 param1
->func
->ident
->name
, param1
->param
);
384 } END_FOR_EACH_PTR(param1
);
386 // if there is an error print it out...
387 FOR_EACH_PTR(calls
, param1
) {
388 FOR_EACH_PTR(do_not_call
, param2
) {
389 if (param1
->func
== param2
->func
&&
390 param1
->param
== param2
->param
)
391 printf("%s +%d cross_func deref %s %d\n",
392 get_filename(), param1
->line
,
393 param1
->func
->ident
->name
,
395 } END_FOR_EACH_PTR(param2
);
396 } END_FOR_EACH_PTR(param1
);
398 // if someone calls a non-static function print that..
399 FOR_EACH_PTR(calls
, param1
) {
402 FOR_EACH_PTR(funcs
, param2
) {
403 if (param1
->func
== param2
->func
)
405 } END_FOR_EACH_PTR(param2
);
407 printf("%s +%d undefined param %s %d\n", get_filename(),
408 param1
->line
, param1
->func
->ident
->name
,
410 } END_FOR_EACH_PTR(param1
);
413 void register_null_deref(int id
)
416 add_merge_hook(my_id
, &merge_func
);
417 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
418 add_hook(&match_function_call_after
, FUNCTION_CALL_AFTER_HOOK
);
419 add_hook(&match_assign
, ASSIGNMENT_AFTER_HOOK
);
420 add_hook(&match_condition
, CONDITION_HOOK
);
421 add_hook(&match_dereferences
, DEREF_HOOK
);
422 add_hook(&match_declarations
, DECLARATION_HOOK
);
423 add_hook(&end_file_processing
, END_FILE_HOOK
);