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 void ok_to_use(struct sm_state
*sm
, struct expression
*mod_expr
)
28 set_state(my_id
, sm
->name
, sm
->sym
, &undefined
);
31 static void match_returns_err_ptr(const char *fn
, struct expression
*expr
,
34 set_state_expr(my_id
, expr
->left
, &err_ptr
);
37 static void match_condition(struct expression
*expr
)
40 struct range_list
*rl
;
46 struct range_list
*err_rl
;
48 while (expr
->type
== EXPR_ASSIGNMENT
)
49 expr
= strip_expr(expr
->left
);
51 sm
= get_sm_state_expr(my_id
, expr
);
54 if (!slist_has_state(sm
->possible
, &err_ptr
))
56 if (!get_implied_rl(expr
, &rl
))
58 zero
.type
= rl_type(rl
);
59 if (rl_has_sval(rl
, zero
))
63 * At this point we already know that the condition is bogus because
64 * it's non-NULL. But let's do another filter to make sure it really is
65 * a possible error pointer.
69 err_rl
= alloc_rl(err_min
, err_max
);
70 if (!possibly_true_rl(rl
, SPECIAL_EQUAL
, err_rl
))
73 name
= expr_to_str(expr
);
74 sm_msg("warn: '%s' is an error pointer or valid", name
);
78 static void match_condition2(struct expression
*expr
)
80 struct range_list
*rl
;
81 struct data_range
*drange
;
84 if (!is_pointer(expr
))
86 if (!get_implied_rl(expr
, &rl
))
89 FOR_EACH_PTR(rl
, drange
) {
90 if (sval_cmp(drange
->min
, drange
->max
) != 0)
92 if (drange
->min
.value
>= -4095 && drange
->min
.value
< 0)
94 } END_FOR_EACH_PTR(drange
);
99 name
= expr_to_str(expr
);
101 sm_warning("'%s' could be an error pointer", name
);
105 static void register_err_ptr_funcs(void)
110 token
= get_tokens_file("kernel.returns_err_ptr");
113 if (token_type(token
) != TOKEN_STREAMBEGIN
)
116 while (token_type(token
) != TOKEN_STREAMEND
) {
117 if (token_type(token
) != TOKEN_IDENT
)
119 func
= show_ident(token
->ident
);
120 add_function_assign_hook(func
, &match_returns_err_ptr
, NULL
);
126 void check_checking_for_null_instead_of_err_ptr(int id
)
128 if (option_project
!= PROJ_KERNEL
)
132 register_err_ptr_funcs();
133 add_hook(&match_condition
, CONDITION_HOOK
);
134 add_hook(&match_condition2
, CONDITION_HOOK
);
135 add_modification_hook(my_id
, &ok_to_use
);