2 * Copyright (C) 2017 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 #include "smatch_slist.h"
20 #include "smatch_extra.h"
26 static struct string_list
*ignored_macros
;
28 static int in_ignored_macro(struct position pos
)
33 macro
= get_macro_name(pos
);
37 FOR_EACH_PTR(ignored_macros
, tmp
) {
38 if (!strcmp(tmp
, macro
))
40 } END_FOR_EACH_PTR(tmp
);
44 static void ok_to_use(struct sm_state
*sm
, struct expression
*mod_expr
)
46 set_state(my_id
, sm
->name
, sm
->sym
, &undefined
);
49 static void match_returns_err_ptr(const char *fn
, struct expression
*expr
,
52 set_state_expr(my_id
, expr
->left
, &err_ptr
);
55 static void match_condition(struct expression
*expr
)
58 struct range_list
*rl
;
64 struct range_list
*err_rl
;
66 while (expr
->type
== EXPR_ASSIGNMENT
)
67 expr
= strip_expr(expr
->left
);
69 if (!is_pointer(expr
))
72 sm
= get_sm_state_expr(my_id
, expr
);
75 if (!slist_has_state(sm
->possible
, &err_ptr
))
77 if (!get_implied_rl(expr
, &rl
))
79 zero
.type
= rl_type(rl
);
80 if (rl_has_sval(rl
, zero
))
84 * At this point we already know that the condition is bogus because
85 * it's non-NULL. But let's do another filter to make sure it really is
86 * a possible error pointer.
90 err_rl
= alloc_rl(err_min
, err_max
);
91 if (!possibly_true_rl(rl
, SPECIAL_EQUAL
, err_rl
))
94 if (in_ignored_macro(expr
->pos
))
97 name
= expr_to_str(expr
);
98 sm_msg("warn: '%s' is an error pointer or valid", name
);
102 static void match_condition2(struct expression
*expr
)
104 struct range_list
*rl
;
105 struct data_range
*drange
;
108 if (!is_pointer(expr
))
110 if (!get_implied_rl(expr
, &rl
))
113 FOR_EACH_PTR(rl
, drange
) {
114 if (sval_cmp(drange
->min
, drange
->max
) != 0)
116 if (drange
->min
.value
>= -4095 && drange
->min
.value
< 0)
118 } END_FOR_EACH_PTR(drange
);
123 name
= expr_to_str(expr
);
124 sm_warning("'%s' could be an error pointer", name
);
128 static void register_err_ptr_funcs(void)
133 token
= get_tokens_file("kernel.returns_err_ptr");
136 if (token_type(token
) != TOKEN_STREAMBEGIN
)
139 while (token_type(token
) != TOKEN_STREAMEND
) {
140 if (token_type(token
) != TOKEN_IDENT
)
142 func
= show_ident(token
->ident
);
143 add_function_assign_hook(func
, &match_returns_err_ptr
, NULL
);
149 static void register_ignored_macros(void)
155 snprintf(name
, 256, "%s.ignore_bogus_null_checks", option_project_str
);
157 token
= get_tokens_file(name
);
160 if (token_type(token
) != TOKEN_STREAMBEGIN
)
163 while (token_type(token
) != TOKEN_STREAMEND
) {
164 if (token_type(token
) != TOKEN_IDENT
)
166 macro
= alloc_string(show_ident(token
->ident
));
167 add_ptr_list(&ignored_macros
, macro
);
173 void check_checking_for_null_instead_of_err_ptr(int id
)
175 if (option_project
!= PROJ_KERNEL
)
179 register_err_ptr_funcs();
180 add_hook(&match_condition
, CONDITION_HOOK
);
182 add_hook(&match_condition2
, CONDITION_HOOK
);
183 add_modification_hook(my_id
, &ok_to_use
);
184 register_ignored_macros();