2 * Copyright (C) 2009 Dan Carpenter.
3 * Copyright (C) 2022 Oracle.
4 * Copyright 2023 Linaro Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
21 #include "smatch_extra.h"
25 static int cap_gt_zero_and_lt(struct expression
*expr
)
28 struct expression
*var
= expr
->left
;
29 struct expression
*tmp
;
36 if (!get_value(expr
->right
, &known
) || known
.value
!= 0)
40 FOR_EACH_PTR_REVERSE(big_expression_stack
, tmp
) {
43 if (tmp
->op
== SPECIAL_LOGICAL_AND
) {
44 struct expression
*right
= strip_expr(tmp
->right
);
46 if (right
->op
!= '<' &&
47 right
->op
!= SPECIAL_UNSIGNED_LT
&&
48 right
->op
!= SPECIAL_LTE
&&
49 right
->op
!= SPECIAL_UNSIGNED_LTE
)
52 name1
= expr_to_str(var
);
56 name2
= expr_to_str(right
->left
);
59 if (!strcmp(name1
, name2
))
65 } END_FOR_EACH_PTR_REVERSE(tmp
);
73 static int cap_lt_zero_or_gt(struct expression
*expr
)
76 struct expression
*var
= expr
->left
;
77 struct expression
*tmp
;
84 if (!get_value(expr
->right
, &known
) || known
.value
!= 0)
88 FOR_EACH_PTR_REVERSE(big_expression_stack
, tmp
) {
91 if (tmp
->op
== SPECIAL_LOGICAL_OR
) {
92 struct expression
*right
= strip_expr(tmp
->right
);
94 if (right
->op
!= '>' &&
95 right
->op
!= SPECIAL_UNSIGNED_GT
&&
96 right
->op
!= SPECIAL_GTE
&&
97 right
->op
!= SPECIAL_UNSIGNED_GTE
)
100 name1
= expr_to_str(var
);
104 name2
= expr_to_str(right
->left
);
107 if (!strcmp(name1
, name2
))
113 } END_FOR_EACH_PTR_REVERSE(tmp
);
121 static int cap_both_sides(struct expression
*expr
)
125 case SPECIAL_UNSIGNED_LT
:
127 case SPECIAL_UNSIGNED_LTE
:
128 return cap_lt_zero_or_gt(expr
);
130 case SPECIAL_UNSIGNED_GT
:
132 case SPECIAL_UNSIGNED_GTE
:
133 return cap_gt_zero_and_lt(expr
);
138 static int compare_against_macro(struct expression
*expr
)
142 if (expr
->op
!= SPECIAL_UNSIGNED_LT
)
145 if (!get_value(expr
->right
, &known
) || known
.value
!= 0)
147 return !!get_macro_name(expr
->right
->pos
);
150 static void match_condition(struct expression
*expr
)
155 struct range_list
*rl_left_orig
, *rl_right_orig
;
156 struct range_list
*rl_left
, *rl_right
;
158 if (expr
->type
!= EXPR_COMPARE
)
161 type
= get_type(expr
);
165 /* screw it. I am writing this to mark yoda code as buggy.
166 * Valid comparisons between an unsigned and zero are:
168 * 2) foo < LOWER_BOUND where LOWER_BOUND is a macro.
169 * 3) foo < 0 || foo > X in exactly this format. No Yoda.
170 * 4) foo >= 0 && foo < X
172 if (get_macro_name(expr
->pos
))
174 if (compare_against_macro(expr
))
176 if (cap_both_sides(expr
))
179 /* check that one and only one side is known */
180 if (get_value(expr
->left
, &known
)) {
181 if (get_value(expr
->right
, &known
))
183 rl_left_orig
= alloc_rl(known
, known
);
184 rl_left
= cast_rl(type
, rl_left_orig
);
186 min
= sval_type_min(get_type(expr
->right
));
187 max
= sval_type_max(get_type(expr
->right
));
188 rl_right_orig
= alloc_rl(min
, max
);
189 rl_right
= cast_rl(type
, rl_right_orig
);
190 } else if (get_value(expr
->right
, &known
)) {
191 rl_right_orig
= alloc_rl(known
, known
);
192 rl_right
= cast_rl(type
, rl_right_orig
);
194 min
= sval_type_min(get_type(expr
->left
));
195 max
= sval_type_max(get_type(expr
->left
));
196 rl_left_orig
= alloc_rl(min
, max
);
197 rl_left
= cast_rl(type
, rl_left_orig
);
202 if (!possibly_false_rl(rl_left
, expr
->op
, rl_right
) &&
203 !is_unconstant_macro(expr
->left
) &&
204 !is_unconstant_macro(expr
->right
)) {
205 char *name
= expr_to_str(expr
);
207 sm_warning("always true condition '(%s) => (%s %s %s)'", name
,
208 show_rl(rl_left_orig
), show_special(expr
->op
),
209 show_rl(rl_right_orig
));
214 void check_always_true(int id
)
218 add_hook(&match_condition
, CONDITION_HOOK
);