Print this page
new smatch
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/smatch/src/check_dereferences_param.c
+++ new/usr/src/tools/smatch/src/check_dereferences_param.c
1 1 /*
2 2 * Copyright (C) 2012 Oracle.
3 3 *
4 4 * This program is free software; you can redistribute it and/or
5 5 * modify it under the terms of the GNU General Public License
6 6 * as published by the Free Software Foundation; either version 2
7 7 * of the License, or (at your option) any later version.
8 8 *
9 9 * This program is distributed in the hope that it will be useful,
10 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 * GNU General Public License for more details.
13 13 *
14 14 * You should have received a copy of the GNU General Public License
15 15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16 16 */
17 17
18 18 /*
19 19 * This is an --info recipe. The goal is to print a message for every parameter
20 20 * which we can not avoid dereferencing. This is maybe a bit restrictive but it
21 21 * avoids some false positives.
22 22 */
23 23
24 24 #include "smatch.h"
25 25 #include "smatch_extra.h"
26 26 #include "smatch_slist.h"
27 27
28 28 static int my_id;
29 29
30 30 STATE(derefed);
31 31 STATE(ignore);
32 32 STATE(param);
33 33
34 34 static void set_ignore(struct sm_state *sm, struct expression *mod_expr)
35 35 {
36 36 if (sm->state == &derefed)
37 37 return;
38 38 set_state(my_id, sm->name, sm->sym, &ignore);
39 39 }
40 40
41 41 static void match_function_def(struct symbol *sym)
42 42 {
43 43 struct symbol *arg;
44 44 int i;
45 45
46 46 i = -1;
47 47 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
48 48 i++;
49 49 if (!arg->ident)
50 50 continue;
51 51 set_state(my_id, arg->ident->name, arg, ¶m);
52 52 } END_FOR_EACH_PTR(arg);
53 53 }
54 54
55 55 static void check_deref(struct expression *expr)
56 56 {
57 57 struct expression *tmp;
58 58 struct sm_state *sm;
59 59
60 60 tmp = get_assigned_expr(expr);
61 61 if (tmp)
62 62 expr = tmp;
63 63 expr = strip_expr(expr);
64 64
65 65 if (get_param_num(expr) < 0)
66 66 return;
67 67
68 68 if (param_was_set(expr))
69 69 return;
70 70
↓ open down ↓ |
70 lines elided |
↑ open up ↑ |
71 71 sm = get_sm_state_expr(my_id, expr);
72 72 if (sm && slist_has_state(sm->possible, &ignore))
73 73 return;
74 74 set_state_expr(my_id, expr, &derefed);
75 75 }
76 76
77 77 static void match_dereference(struct expression *expr)
78 78 {
79 79 if (expr->type != EXPR_PREOP)
80 80 return;
81 - if (getting_address())
82 - return;
83 81 check_deref(expr->unop);
84 82 }
85 83
86 84 static void set_param_dereferenced(struct expression *call, struct expression *arg, char *key, char *unused)
87 85 {
88 86 /* XXX FIXME: param_implies has more information now */
89 87 if (strcmp(key, "$") != 0)
90 88 return;
91 89 check_deref(arg);
92 90 }
93 91
94 92 static void process_states(void)
95 93 {
96 94 struct sm_state *tmp;
97 95 int arg;
98 96 const char *name;
99 97
100 98 FOR_EACH_MY_SM(my_id, __get_cur_stree(), tmp) {
101 99 if (tmp->state != &derefed)
102 100 continue;
103 101 arg = get_param_num_from_sym(tmp->sym);
104 102 if (arg < 0)
105 103 continue;
106 104 name = get_param_name(tmp);
107 105 if (!name)
108 106 continue;
109 107 sql_insert_return_implies(DEREFERENCE, arg, name, "1");
110 108 } END_FOR_EACH_SM(tmp);
111 109 }
112 110
113 111 static void match_pointer_as_array(struct expression *expr)
114 112 {
115 113 if (!is_array(expr))
116 114 return;
117 115 check_deref(get_array_base(expr));
118 116 }
119 117
120 118 void check_dereferences_param(int id)
121 119 {
122 120 my_id = id;
123 121
124 122 add_hook(&match_function_def, FUNC_DEF_HOOK);
125 123
126 124 add_hook(&match_dereference, DEREF_HOOK);
127 125 add_hook(&match_pointer_as_array, OP_HOOK);
128 126 select_return_implies_hook(DEREFERENCE, &set_param_dereferenced);
129 127 add_modification_hook(my_id, &set_ignore);
130 128
131 129 all_return_states_hook(&process_states);
132 130 }
↓ open down ↓ |
40 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX