2 * Copyright (C) 2013 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_extra.h"
20 #include "smatch_slist.h"
24 static int is_comparison_call(struct expression
*expr
)
26 expr
= expr_get_parent_expr(expr
);
27 if (!expr
|| expr
->type
!= EXPR_COMPARE
)
29 if (expr
->op
!= SPECIAL_EQUAL
&& expr
->op
!= SPECIAL_NOTEQUAL
)
34 static int next_line_is_if(struct expression
*expr
)
36 struct expression
*next
;
38 if (!__next_stmt
|| __next_stmt
->type
!= STMT_IF
)
41 next
= strip_expr(__next_stmt
->if_conditional
);
42 while (next
->type
== EXPR_PREOP
&& next
->op
== '!')
43 next
= strip_expr(next
->unop
);
44 if (expr_equiv(expr
, next
))
49 static int next_line_checks_IS_ERR(struct expression
*call
, struct expression
*arg
)
51 struct expression
*next
;
52 struct expression
*tmp
;
54 tmp
= expr_get_parent_expr(call
);
55 if (tmp
&& tmp
->type
== EXPR_ASSIGNMENT
) {
56 if (next_line_checks_IS_ERR(NULL
, tmp
->left
))
60 if (!__next_stmt
|| __next_stmt
->type
!= STMT_IF
)
63 next
= strip_expr(__next_stmt
->if_conditional
);
64 while (next
->type
== EXPR_PREOP
&& next
->op
== '!')
65 next
= strip_expr(next
->unop
);
66 if (!next
|| next
->type
!= EXPR_CALL
)
68 if (next
->fn
->type
!= EXPR_SYMBOL
|| !next
->fn
->symbol
->ident
||
69 (strcmp(next
->fn
->symbol
->ident
->name
, "IS_ERR") != 0 &&
70 strcmp(next
->fn
->symbol
->ident
->name
, "IS_ERR_OR_NULL") != 0))
72 next
= get_argument_from_call_expr(next
->args
, 0);
73 return expr_equiv(next
, arg
);
76 static int is_valid_ptr(sval_t sval
)
78 if (sval
.type
== &int_ctype
&&
79 (sval
.value
== INT_MIN
|| sval
.value
== INT_MAX
))
82 if (sval_cmp(valid_ptr_min_sval
, sval
) <= 0 &&
83 sval_cmp(valid_ptr_max_sval
, sval
) >= 0)
88 static void match_err_ptr(const char *fn
, struct expression
*expr
, void *data
)
90 struct expression
*arg_expr
;
91 struct sm_state
*sm
, *tmp
;
94 arg_expr
= get_argument_from_call_expr(expr
->args
, 0);
95 sm
= get_sm_state_expr(SMATCH_EXTRA
, arg_expr
);
99 if (is_comparison_call(expr
))
102 if (next_line_checks_IS_ERR(expr
, arg_expr
))
104 if (strcmp(fn
, "ERR_PTR") == 0 &&
105 next_line_is_if(arg_expr
))
108 FOR_EACH_PTR(sm
->possible
, tmp
) {
109 if (!estate_rl(tmp
->state
))
111 if (is_valid_ptr(estate_min(tmp
->state
)) &&
112 is_valid_ptr(estate_max(tmp
->state
))) {
113 sm_msg("warn: passing a valid pointer to '%s'", fn
);
116 if (!rl_to_sval(estate_rl(tmp
->state
), &sval
))
120 sm_msg("warn: passing zero to '%s'", fn
);
122 } END_FOR_EACH_PTR(tmp
);
125 void check_zero_to_err_ptr(int id
)
127 if (option_project
!= PROJ_KERNEL
)
131 add_function_hook("ERR_PTR", &match_err_ptr
, NULL
);
132 add_function_hook("ERR_CAST", &match_err_ptr
, NULL
);
133 add_function_hook("PTR_ERR", &match_err_ptr
, NULL
);