7 * of the License, or (at your option) any later version.
8 *
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.
13 *
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
16 */
17
18 #include "smatch.h"
19 #include "smatch_slist.h"
20 #include "smatch_extra.h"
21
22 static int my_id;
23
24 STATE(uninitialized);
25 STATE(initialized);
26
27 static void pre_merge_hook(struct sm_state *sm)
28 {
29 if (is_impossible_path())
30 set_state(my_id, sm->name, sm->sym, &initialized);
31 }
32
33 static void mark_members_uninitialized(struct symbol *sym)
34 {
35 struct symbol *struct_type, *tmp, *base_type;
36 char buf[256];
37
38 struct_type = get_real_base_type(sym);
39 FOR_EACH_PTR(struct_type->symbol_list, tmp) {
40 if (!tmp->ident)
41 continue;
42 base_type = get_real_base_type(tmp);
43 if (!base_type ||
44 base_type->type == SYM_STRUCT ||
45 base_type->type == SYM_ARRAY ||
46 base_type->type == SYM_UNION)
47 continue;
48 snprintf(buf, sizeof(buf), "%s.%s", sym->ident->name, tmp->ident->name);
49 set_state(my_id, buf, sym, &uninitialized);
50 } END_FOR_EACH_PTR(tmp);
96 }
97
98 static void match_negative_comparison(struct expression *expr)
99 {
100 struct expression *success;
101 struct sm_state *sm;
102 sval_t max;
103
104 /*
105 * In the kernel, people don't use "if (ret) {" and "if (ret < 0) {"
106 * consistently. Ideally Smatch would know the return but often it
107 * doesn't.
108 *
109 */
110
111 if (option_project != PROJ_KERNEL)
112 return;
113
114 if (expr->type != EXPR_COMPARE || expr->op != '<')
115 return;
116 if (!is_zero(expr->right))
117 return;
118 if (get_implied_max(expr->left, &max) && max.value == 0)
119 return;
120
121 success = compare_expression(expr->left, SPECIAL_EQUAL, expr->right);
122 if (!assume(success))
123 return;
124
125 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
126 if (sm->state == &initialized)
127 set_true_false_states(my_id, sm->name, sm->sym, NULL, &initialized);
128 } END_FOR_EACH_SM(sm);
129
130 end_assume();
131 }
132
133 static int is_initialized(struct expression *expr)
134 {
135 struct sm_state *sm;
136
|
7 * of the License, or (at your option) any later version.
8 *
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.
13 *
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
16 */
17
18 #include "smatch.h"
19 #include "smatch_slist.h"
20 #include "smatch_extra.h"
21
22 static int my_id;
23
24 STATE(uninitialized);
25 STATE(initialized);
26
27 static void pre_merge_hook(struct sm_state *cur, struct sm_state *other)
28 {
29 if (is_impossible_path())
30 set_state(my_id, cur->name, cur->sym, &initialized);
31 }
32
33 static void mark_members_uninitialized(struct symbol *sym)
34 {
35 struct symbol *struct_type, *tmp, *base_type;
36 char buf[256];
37
38 struct_type = get_real_base_type(sym);
39 FOR_EACH_PTR(struct_type->symbol_list, tmp) {
40 if (!tmp->ident)
41 continue;
42 base_type = get_real_base_type(tmp);
43 if (!base_type ||
44 base_type->type == SYM_STRUCT ||
45 base_type->type == SYM_ARRAY ||
46 base_type->type == SYM_UNION)
47 continue;
48 snprintf(buf, sizeof(buf), "%s.%s", sym->ident->name, tmp->ident->name);
49 set_state(my_id, buf, sym, &uninitialized);
50 } END_FOR_EACH_PTR(tmp);
96 }
97
98 static void match_negative_comparison(struct expression *expr)
99 {
100 struct expression *success;
101 struct sm_state *sm;
102 sval_t max;
103
104 /*
105 * In the kernel, people don't use "if (ret) {" and "if (ret < 0) {"
106 * consistently. Ideally Smatch would know the return but often it
107 * doesn't.
108 *
109 */
110
111 if (option_project != PROJ_KERNEL)
112 return;
113
114 if (expr->type != EXPR_COMPARE || expr->op != '<')
115 return;
116 if (!expr_is_zero(expr->right))
117 return;
118 if (get_implied_max(expr->left, &max) && max.value == 0)
119 return;
120
121 success = compare_expression(expr->left, SPECIAL_EQUAL, expr->right);
122 if (!assume(success))
123 return;
124
125 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
126 if (sm->state == &initialized)
127 set_true_false_states(my_id, sm->name, sm->sym, NULL, &initialized);
128 } END_FOR_EACH_SM(sm);
129
130 end_assume();
131 }
132
133 static int is_initialized(struct expression *expr)
134 {
135 struct sm_state *sm;
136
|