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 if (!is_pointer(expr
))
54 sm
= get_sm_state_expr(my_id
, expr
);
57 if (!slist_has_state(sm
->possible
, &err_ptr
))
59 if (!get_implied_rl(expr
, &rl
))
61 zero
.type
= rl_type(rl
);
62 if (rl_has_sval(rl
, zero
))
66 * At this point we already know that the condition is bogus because
67 * it's non-NULL. But let's do another filter to make sure it really is
68 * a possible error pointer.
72 err_rl
= alloc_rl(err_min
, err_max
);
73 if (!possibly_true_rl(rl
, SPECIAL_EQUAL
, err_rl
))
76 name
= expr_to_str(expr
);
77 sm_msg("warn: '%s' is an error pointer or valid", name
);
81 static void match_condition2(struct expression
*expr
)
83 struct range_list
*rl
;
84 struct data_range
*drange
;
87 if (!is_pointer(expr
))
89 if (!get_implied_rl(expr
, &rl
))
92 FOR_EACH_PTR(rl
, drange
) {
93 if (sval_cmp(drange
->min
, drange
->max
) != 0)
95 if (drange
->min
.value
>= -4095 && drange
->min
.value
< 0)
97 } END_FOR_EACH_PTR(drange
);
102 name
= expr_to_str(expr
);
104 sm_warning("'%s' could be an error pointer", name
);
108 static void register_err_ptr_funcs(void)
113 token
= get_tokens_file("kernel.returns_err_ptr");
116 if (token_type(token
) != TOKEN_STREAMBEGIN
)
119 while (token_type(token
) != TOKEN_STREAMEND
) {
120 if (token_type(token
) != TOKEN_IDENT
)
122 func
= show_ident(token
->ident
);
123 add_function_assign_hook(func
, &match_returns_err_ptr
, NULL
);
129 void check_checking_for_null_instead_of_err_ptr(int id
)
131 if (option_project
!= PROJ_KERNEL
)
135 register_err_ptr_funcs();
136 add_hook(&match_condition
, CONDITION_HOOK
);
137 add_hook(&match_condition2
, CONDITION_HOOK
);
138 add_modification_hook(my_id
, &ok_to_use
);