2 * sparse/check_dereferences_param.c
4 * Copyright (C) 2012 Oracle.
6 * Licensed under the Open Software License version 1.1
11 * This is an --info recipe. The goal is to print a message for every parameter
12 * which we can not avoid dereferencing. This is maybe a bit restrictive but it
13 * avoids some false positives.
17 #include "smatch_extra.h"
18 #include "smatch_slist.h"
26 static int is_arg(struct expression
*expr
)
30 if (expr
->type
!= EXPR_SYMBOL
)
33 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
34 if (arg
== expr
->symbol
)
36 } END_FOR_EACH_PTR(arg
);
40 static void set_ignore(struct sm_state
*sm
)
42 if (sm
->state
== &derefed
)
44 set_state(my_id
, sm
->name
, sm
->sym
, &ignore
);
47 static void match_function_def(struct symbol
*sym
)
53 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
57 set_state(my_id
, arg
->ident
->name
, arg
, ¶m
);
58 } END_FOR_EACH_PTR(arg
);
61 static void check_deref(struct expression
*expr
)
63 struct expression
*tmp
;
66 tmp
= get_assigned_expr(expr
);
69 expr
= strip_expr(expr
);
74 sm
= get_sm_state_expr(my_id
, expr
);
75 if (sm
&& slist_has_state(sm
->possible
, &ignore
))
77 set_state_expr(my_id
, expr
, &derefed
);
80 static void match_dereference(struct expression
*expr
)
82 if (expr
->type
!= EXPR_PREOP
)
84 if (getting_address())
86 check_deref(expr
->unop
);
89 static void set_param_dereferenced(struct expression
*arg
, char *unused
)
94 static void process_states(struct state_list
*slist
)
100 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
104 if (get_state_slist(slist
, my_id
, arg
->ident
->name
, arg
) == &derefed
)
105 sql_insert_call_implies(DEREFERENCE
, i
, 1);
106 } END_FOR_EACH_PTR(arg
);
109 void check_dereferences_param(int id
)
113 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
115 add_hook(&match_dereference
, DEREF_HOOK
);
116 add_db_fn_call_callback(DEREFERENCE
, &set_param_dereferenced
);
117 add_modification_hook(my_id
, &set_ignore
);
119 all_return_states_hook(&process_states
);