2 * Copyright (C) 2012 Oracle.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * This is an --info recipe. The goal is to print a message for every parameter
20 * which we can not avoid dereferencing. This is maybe a bit restrictive but it
21 * avoids some false positives.
25 #include "smatch_extra.h"
26 #include "smatch_slist.h"
34 static void set_ignore(struct sm_state
*sm
, struct expression
*mod_expr
)
36 if (sm
->state
== &derefed
)
38 set_state(my_id
, sm
->name
, sm
->sym
, &ignore
);
41 static void match_function_def(struct symbol
*sym
)
47 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
51 set_state(my_id
, arg
->ident
->name
, arg
, ¶m
);
52 } END_FOR_EACH_PTR(arg
);
55 static void check_deref(struct expression
*expr
)
57 struct expression
*tmp
;
60 tmp
= get_assigned_expr(expr
);
63 expr
= strip_expr(expr
);
65 if (get_param_num(expr
) < 0)
68 if (param_was_set(expr
))
71 sm
= get_sm_state_expr(my_id
, expr
);
72 if (sm
&& slist_has_state(sm
->possible
, &ignore
))
74 set_state_expr(my_id
, expr
, &derefed
);
77 static void match_dereference(struct expression
*expr
)
79 if (expr
->type
!= EXPR_PREOP
)
81 if (getting_address())
83 check_deref(expr
->unop
);
86 static void set_param_dereferenced(struct expression
*call
, struct expression
*arg
, char *key
, char *unused
)
88 /* XXX FIXME: param_implies has more information now */
89 if (strcmp(key
, "$") != 0)
94 static void process_states(struct stree
*stree
)
100 FOR_EACH_MY_SM(my_id
, stree
, tmp
) {
101 if (tmp
->state
!= &derefed
)
103 arg
= get_param_num_from_sym(tmp
->sym
);
106 name
= get_param_name(tmp
);
109 sql_insert_call_implies(DEREFERENCE
, arg
, name
, "1");
110 } END_FOR_EACH_SM(tmp
);
113 static void match_pointer_as_array(struct expression
*expr
)
117 check_deref(get_array_base(expr
));
120 void check_dereferences_param(int id
)
124 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
126 add_hook(&match_dereference
, DEREF_HOOK
);
127 add_hook(&match_pointer_as_array
, OP_HOOK
);
128 select_call_implies_hook(DEREFERENCE
, &set_param_dereferenced
);
129 add_modification_hook(my_id
, &set_ignore
);
131 all_return_states_hook(&process_states
);