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
);
201 name
= get_variable_from_expr(tmp
, &sym
);
202 if (name
&& is_maybe_null(name
, sym
))
203 add_param(&calls
, func
, i
, get_lineno());
207 } END_FOR_EACH_PTR(tmp
);
210 static int check_null_returns(const char *name
, struct symbol
*sym
,
211 struct expression
*right
)
216 func_name
= get_function_call(right
);
220 for(i
= 0; i
< sizeof(*return_null
)/sizeof(return_null
[0]); i
++) {
221 if (!strcmp(func_name
, return_null
[i
])) {
222 set_state(name
, my_id
, sym
, &undefined
);
229 static int assign_seen
;
230 static void match_assign(struct expression
*expr
)
232 struct expression
*left
, *right
;
240 left
= strip_expr(expr
->left
);
241 name
= get_variable_from_expr(left
, &sym
);
244 right
= strip_expr(expr
->right
);
245 if (is_zero(right
)) {
246 set_state(name
, my_id
, sym
, &isnull
);
250 if (check_null_returns(name
, sym
, right
)) {
255 /* by default we assume it's assigned something nonnull */
256 set_state(name
, my_id
, sym
, &assumed_nonnull
);
261 * set_new_true_false_paths() is used in the following conditions
262 * if (a) { ... if (a) { ... } }
263 * The problem is that after the second blog a is set to undefined
264 * even though the second condition is meaning less. (The second test
265 * could be a macro for example).
267 * Also, stuff passed to the condition hook is processed behind the scenes
268 * a bit. Instead of a condition like (foo == 0) which is true when foo is
269 * null, we get just (foo) and the true and false states are adjusted later.
270 * Basically the important thing to remember, is that for us, true is always
271 * non null and false is always null.
273 static void set_new_true_false_paths(const char *name
, struct symbol
*sym
)
275 struct smatch_state
*tmp
;
277 tmp
= get_state(name
, my_id
, sym
);
279 SM_DEBUG("set_new_stuff called at for %s on line %d value='%s'\n",
280 name
, get_lineno(), show_state(tmp
));
282 if (tmp
== &argument
) {
283 set_true_false_states(name
, my_id
, sym
, &arg_nonnull
, &arg_null
);
287 if (!tmp
|| is_maybe_null(name
, sym
)) {
288 set_true_false_states(name
, my_id
, sym
, &nonnull
, &isnull
);
294 static void match_condition(struct expression
*expr
)
299 expr
= strip_expr(expr
);
304 name
= get_variable_from_expr(expr
, &sym
);
307 set_new_true_false_paths(name
, sym
);
310 case EXPR_ASSIGNMENT
:
313 * There is a kernel macro that does
314 * for ( ... ; ... || x = NULL ; ) ...
316 if (is_zero(expr
->right
)) {
317 name
= get_variable_from_expr(expr
->left
, &sym
);
320 set_true_false_states(name
, my_id
, sym
, NULL
, &isnull
);
324 /* You have to deal with stuff like if (a = b = c) */
325 match_condition(expr
->right
);
326 match_condition(expr
->left
);
333 static void match_declarations(struct symbol
*sym
)
337 if ((get_base_type(sym
))->type
== SYM_ARRAY
) {
341 name
= sym
->ident
->name
;
343 if (sym
->initializer
) {
344 if (is_zero(sym
->initializer
)) {
345 set_state(name
, my_id
, sym
, &isnull
);
348 if (check_null_returns(name
, sym
, strip_expr(sym
->initializer
)))
350 set_state(name
, my_id
, sym
, &assumed_nonnull
);
352 set_state(name
, my_id
, sym
, &undefined
);
356 static void match_dereferences(struct expression
*expr
)
359 struct symbol
*sym
= NULL
;
361 if (strcmp(show_special(expr
->deref
->op
), "*"))
364 deref
= get_variable_from_expr(expr
->deref
->unop
, &sym
);
368 if (is_argument(deref
, sym
)) {
369 add_do_not_call(sym
, get_lineno());
370 set_state(deref
, my_id
, sym
, &assumed_nonnull
);
371 } else if (is_maybe_null(deref
, sym
)) {
372 smatch_msg("Dereferencing Undefined: '%s'", deref
);
373 set_state(deref
, my_id
, sym
, &ignore
);
378 static void end_file_processing()
380 struct func_n_param
*param1
, *param2
;
382 // if a function is not static print it out...
383 FOR_EACH_PTR(do_not_call
, param1
) {
384 if (!(param1
->func
->ctype
.modifiers
& MOD_STATIC
))
385 printf("%s +%d unchecked param %s %d\n",
386 get_filename(), param1
->line
,
387 param1
->func
->ident
->name
, param1
->param
);
388 } END_FOR_EACH_PTR(param1
);
390 // if there is an error print it out...
391 FOR_EACH_PTR(calls
, param1
) {
392 FOR_EACH_PTR(do_not_call
, param2
) {
393 if (param1
->func
== param2
->func
&&
394 param1
->param
== param2
->param
)
395 printf("%s +%d cross_func deref %s %d\n",
396 get_filename(), param1
->line
,
397 param1
->func
->ident
->name
,
399 } END_FOR_EACH_PTR(param2
);
400 } END_FOR_EACH_PTR(param1
);
402 // if someone calls a non-static function print that..
403 FOR_EACH_PTR(calls
, param1
) {
406 FOR_EACH_PTR(funcs
, param2
) {
407 if (param1
->func
== param2
->func
)
409 } END_FOR_EACH_PTR(param2
);
411 printf("%s +%d undefined param %s %d\n", get_filename(),
412 param1
->line
, param1
->func
->ident
->name
,
414 } END_FOR_EACH_PTR(param1
);
417 void register_null_deref(int id
)
420 add_merge_hook(my_id
, &merge_func
);
421 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
422 add_hook(&match_function_call_after
, FUNCTION_CALL_AFTER_HOOK
);
423 add_hook(&match_assign
, ASSIGNMENT_AFTER_HOOK
);
424 add_hook(&match_condition
, CONDITION_HOOK
);
425 add_hook(&match_dereferences
, DEREF_HOOK
);
426 add_hook(&match_declarations
, DECLARATION_HOOK
);
427 add_hook(&end_file_processing
, END_FILE_HOOK
);