2 * Copyright (C) 2012 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 * The point here is to store the relationships between two variables.
21 * To do that we create a state with the two variables in alphabetical order:
22 * ->name = "x vs y" and the state would be "<". On the false path the state
25 * Part of the trick of it is that if x or y is modified then we need to reset
26 * the state. We need to keep a list of all the states which depend on x and
27 * all the states which depend on y. The link_id code handles this.
32 #include "smatch_extra.h"
33 #include "smatch_slist.h"
35 static int compare_id
;
37 static int inc_dec_id
;
38 static int inc_dec_link_id
;
40 /* for handling for loops */
44 ALLOCATOR(compare_data
, "compare data");
46 static struct symbol
*vsl_to_sym(struct var_sym_list
*vsl
)
52 if (ptr_list_size((struct ptr_list
*)vsl
) != 1)
54 vs
= first_ptr_list((struct ptr_list
*)vsl
);
58 struct smatch_state
*alloc_compare_state(
59 const char *var1
, struct var_sym_list
*vsl1
,
61 const char *var2
, struct var_sym_list
*vsl2
)
63 struct smatch_state
*state
;
64 struct compare_data
*data
;
66 state
= __alloc_smatch_state(0);
67 state
->name
= alloc_sname(show_special(comparison
));
68 data
= __alloc_compare_data(0);
69 data
->var1
= alloc_sname(var1
);
70 data
->vsl1
= clone_var_sym_list(vsl1
);
71 data
->comparison
= comparison
;
72 data
->var2
= alloc_sname(var2
);
73 data
->vsl2
= clone_var_sym_list(vsl2
);
78 int state_to_comparison(struct smatch_state
*state
)
80 if (!state
|| !state
->data
)
82 return ((struct compare_data
*)state
->data
)->comparison
;
86 * flip_comparison() reverses the op left and right. So "x >= y" becomes "y <= x".
88 int flip_comparison(int op
)
95 case SPECIAL_UNSIGNED_LT
:
96 return SPECIAL_UNSIGNED_GT
;
99 case SPECIAL_UNSIGNED_LTE
:
100 return SPECIAL_UNSIGNED_GTE
;
102 return SPECIAL_EQUAL
;
103 case SPECIAL_NOTEQUAL
:
104 return SPECIAL_NOTEQUAL
;
107 case SPECIAL_UNSIGNED_GTE
:
108 return SPECIAL_UNSIGNED_LTE
;
111 case SPECIAL_UNSIGNED_GT
:
112 return SPECIAL_UNSIGNED_LT
;
114 sm_msg("internal smatch bug. unhandled comparison %d", op
);
119 int negate_comparison(int op
)
126 case SPECIAL_UNSIGNED_LT
:
127 return SPECIAL_UNSIGNED_GTE
;
130 case SPECIAL_UNSIGNED_LTE
:
131 return SPECIAL_UNSIGNED_GT
;
133 return SPECIAL_NOTEQUAL
;
134 case SPECIAL_NOTEQUAL
:
135 return SPECIAL_EQUAL
;
138 case SPECIAL_UNSIGNED_GTE
:
139 return SPECIAL_UNSIGNED_LT
;
142 case SPECIAL_UNSIGNED_GT
:
143 return SPECIAL_UNSIGNED_LTE
;
145 sm_msg("internal smatch bug. unhandled comparison %d", op
);
150 static int rl_comparison(struct range_list
*left_rl
, struct range_list
*right_rl
)
152 sval_t left_min
, left_max
, right_min
, right_max
;
154 if (!left_rl
|| !right_rl
)
157 left_min
= rl_min(left_rl
);
158 left_max
= rl_max(left_rl
);
159 right_min
= rl_min(right_rl
);
160 right_max
= rl_max(right_rl
);
162 if (left_min
.value
== left_max
.value
&&
163 right_min
.value
== right_max
.value
&&
164 left_min
.value
== right_min
.value
)
165 return SPECIAL_EQUAL
;
167 if (sval_cmp(left_max
, right_min
) < 0)
169 if (sval_cmp(left_max
, right_min
) == 0)
171 if (sval_cmp(left_min
, right_max
) > 0)
173 if (sval_cmp(left_min
, right_max
) == 0)
179 static struct range_list
*get_orig_rl(struct var_sym_list
*vsl
)
182 struct smatch_state
*state
;
186 sym
= vsl_to_sym(vsl
);
187 if (!sym
|| !sym
->ident
)
189 state
= get_orig_estate(sym
->ident
->name
, sym
);
190 return estate_rl(state
);
193 static struct smatch_state
*unmatched_comparison(struct sm_state
*sm
)
195 struct compare_data
*data
= sm
->state
->data
;
196 struct range_list
*left_rl
, *right_rl
;
202 if (strstr(data
->var1
, " orig"))
203 left_rl
= get_orig_rl(data
->vsl1
);
204 else if (!get_implied_rl_var_sym(data
->var1
, vsl_to_sym(data
->vsl1
), &left_rl
))
206 if (strstr(data
->var2
, " orig"))
207 right_rl
= get_orig_rl(data
->vsl2
);
208 else if (!get_implied_rl_var_sym(data
->var2
, vsl_to_sym(data
->vsl2
), &right_rl
))
212 op
= rl_comparison(left_rl
, right_rl
);
214 return alloc_compare_state(data
->var1
, data
->vsl1
, op
, data
->var2
, data
->vsl2
);
219 /* remove_unsigned_from_comparison() is obviously a hack. */
220 static int remove_unsigned_from_comparison(int op
)
223 case SPECIAL_UNSIGNED_LT
:
225 case SPECIAL_UNSIGNED_LTE
:
227 case SPECIAL_UNSIGNED_GTE
:
229 case SPECIAL_UNSIGNED_GT
:
237 * This is for when you merge states "a < b" and "a == b", the result is that
238 * we can say for sure, "a <= b" after the merge.
240 int merge_comparisons(int one
, int two
)
247 one
= remove_unsigned_from_comparison(one
);
248 two
= remove_unsigned_from_comparison(two
);
298 return SPECIAL_NOTEQUAL
;
309 * This is for if you have "a < b" and "b <= c". Or in other words,
310 * "a < b <= c". You would call this like get_combined_comparison('<', '<=').
311 * The return comparison would be '<'.
313 * This function is different from merge_comparisons(), for example:
314 * merge_comparison('<', '==') returns '<='
315 * get_combined_comparison('<', '==') returns '<'
317 int combine_comparisons(int left_compare
, int right_compare
)
321 left_compare
= remove_unsigned_from_comparison(left_compare
);
322 right_compare
= remove_unsigned_from_comparison(right_compare
);
326 switch (left_compare
) {
335 return right_compare
;
344 switch (right_compare
) {
376 static int filter_comparison(int orig
, int op
)
388 case SPECIAL_NOTEQUAL
:
398 case SPECIAL_NOTEQUAL
:
407 case SPECIAL_UNSIGNED_LTE
:
408 case SPECIAL_UNSIGNED_GTE
:
409 return SPECIAL_EQUAL
;
412 case SPECIAL_NOTEQUAL
:
417 case SPECIAL_UNSIGNED_LT
:
418 case SPECIAL_UNSIGNED_LTE
:
419 return SPECIAL_UNSIGNED_LT
;
420 case SPECIAL_NOTEQUAL
:
425 case SPECIAL_UNSIGNED_GT
:
426 case SPECIAL_UNSIGNED_GTE
:
427 return SPECIAL_UNSIGNED_GT
;
433 return SPECIAL_EQUAL
;
438 case SPECIAL_NOTEQUAL
:
446 case SPECIAL_NOTEQUAL
:
450 case SPECIAL_UNSIGNED_LT
:
452 case SPECIAL_UNSIGNED_LT
:
453 case SPECIAL_UNSIGNED_LTE
:
454 case SPECIAL_NOTEQUAL
:
455 return SPECIAL_UNSIGNED_LT
;
458 case SPECIAL_UNSIGNED_LTE
:
460 case SPECIAL_UNSIGNED_LT
:
461 case SPECIAL_UNSIGNED_LTE
:
464 case SPECIAL_NOTEQUAL
:
465 return SPECIAL_UNSIGNED_LT
;
466 case SPECIAL_UNSIGNED_GTE
:
467 return SPECIAL_EQUAL
;
470 case SPECIAL_UNSIGNED_GTE
:
472 case SPECIAL_UNSIGNED_LTE
:
473 return SPECIAL_EQUAL
;
474 case SPECIAL_NOTEQUAL
:
475 return SPECIAL_UNSIGNED_GT
;
477 case SPECIAL_UNSIGNED_GTE
:
478 case SPECIAL_UNSIGNED_GT
:
482 case SPECIAL_UNSIGNED_GT
:
484 case SPECIAL_UNSIGNED_GT
:
485 case SPECIAL_UNSIGNED_GTE
:
486 case SPECIAL_NOTEQUAL
:
487 return SPECIAL_UNSIGNED_GT
;
494 struct smatch_state
*merge_compare_states(struct smatch_state
*s1
, struct smatch_state
*s2
)
496 struct compare_data
*data
= s1
->data
;
499 op
= merge_comparisons(state_to_comparison(s1
), state_to_comparison(s2
));
501 return alloc_compare_state(data
->var1
, data
->vsl1
, op
, data
->var2
, data
->vsl2
);
505 static struct smatch_state
*alloc_link_state(struct string_list
*links
)
507 struct smatch_state
*state
;
508 static char buf
[256];
512 state
= __alloc_smatch_state(0);
515 FOR_EACH_PTR(links
, tmp
) {
517 snprintf(buf
, sizeof(buf
), "%s", tmp
);
519 append(buf
, ", ", sizeof(buf
));
520 append(buf
, tmp
, sizeof(buf
));
522 } END_FOR_EACH_PTR(tmp
);
524 state
->name
= alloc_sname(buf
);
529 static void save_start_states(struct statement
*stmt
)
531 struct symbol
*param
;
533 char state_name
[128];
534 struct smatch_state
*state
;
535 struct string_list
*links
;
538 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
539 struct var_sym_list
*vsl1
= NULL
;
540 struct var_sym_list
*vsl2
= NULL
;
544 snprintf(orig
, sizeof(orig
), "%s orig", param
->ident
->name
);
545 snprintf(state_name
, sizeof(state_name
), "%s vs %s", param
->ident
->name
, orig
);
546 add_var_sym(&vsl1
, param
->ident
->name
, param
);
547 add_var_sym(&vsl2
, orig
, param
);
548 state
= alloc_compare_state(param
->ident
->name
, vsl1
, SPECIAL_EQUAL
, alloc_sname(orig
), vsl2
);
549 set_state(compare_id
, state_name
, NULL
, state
);
551 link
= alloc_sname(state_name
);
553 insert_string(&links
, link
);
554 state
= alloc_link_state(links
);
555 set_state(link_id
, param
->ident
->name
, param
, state
);
556 } END_FOR_EACH_PTR(param
);
559 static struct smatch_state
*merge_links(struct smatch_state
*s1
, struct smatch_state
*s2
)
561 struct smatch_state
*ret
;
562 struct string_list
*links
;
564 links
= combine_string_lists(s1
->data
, s2
->data
);
565 ret
= alloc_link_state(links
);
569 static void save_link_var_sym(const char *var
, struct symbol
*sym
, const char *link
)
571 struct smatch_state
*old_state
, *new_state
;
572 struct string_list
*links
;
575 old_state
= get_state(link_id
, var
, sym
);
577 links
= clone_str_list(old_state
->data
);
581 new = alloc_sname(link
);
582 insert_string(&links
, new);
584 new_state
= alloc_link_state(links
);
585 set_state(link_id
, var
, sym
, new_state
);
588 static void match_inc(struct sm_state
*sm
)
590 struct string_list
*links
;
591 struct smatch_state
*state
;
596 links
= sm
->state
->data
;
598 FOR_EACH_PTR(links
, tmp
) {
599 state
= get_state(compare_id
, tmp
, NULL
);
602 if (strncmp(sm
->name
, tmp
, strlen(sm
->name
)) != 0 ||
603 tmp
[strlen(sm
->name
)] != ' ')
606 op
= state_to_comparison(state
);
608 switch (flip
? flip_comparison(op
) : op
) {
611 case SPECIAL_UNSIGNED_GTE
:
613 case SPECIAL_UNSIGNED_GT
: {
614 struct compare_data
*data
= state
->data
;
615 struct smatch_state
*new;
617 new = alloc_compare_state(data
->var1
, data
->vsl1
,
619 data
->var2
, data
->vsl2
);
620 set_state(compare_id
, tmp
, NULL
, new);
624 set_state(compare_id
, tmp
, NULL
, &undefined
);
626 } END_FOR_EACH_PTR(tmp
);
629 static void match_dec(struct sm_state
*sm
)
631 struct string_list
*links
;
632 struct smatch_state
*state
;
635 links
= sm
->state
->data
;
637 FOR_EACH_PTR(links
, tmp
) {
638 state
= get_state(compare_id
, tmp
, NULL
);
640 switch (state_to_comparison(state
)) {
643 case SPECIAL_UNSIGNED_LTE
:
645 case SPECIAL_UNSIGNED_LT
: {
646 struct compare_data
*data
= state
->data
;
647 struct smatch_state
*new;
649 new = alloc_compare_state(data
->var1
, data
->vsl1
, '<', data
->var2
, data
->vsl2
);
650 set_state(compare_id
, tmp
, NULL
, new);
654 set_state(compare_id
, tmp
, NULL
, &undefined
);
656 } END_FOR_EACH_PTR(tmp
);
659 static int match_inc_dec(struct sm_state
*sm
, struct expression
*mod_expr
)
663 if (mod_expr
->type
!= EXPR_PREOP
&& mod_expr
->type
!= EXPR_POSTOP
)
666 if (mod_expr
->op
== SPECIAL_INCREMENT
) {
670 if (mod_expr
->op
== SPECIAL_DECREMENT
) {
677 static void match_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
679 struct string_list
*links
;
683 * if (foo > bar) then ++foo is also > bar.
685 if (match_inc_dec(sm
, mod_expr
))
688 links
= sm
->state
->data
;
690 FOR_EACH_PTR(links
, tmp
) {
691 set_state(compare_id
, tmp
, NULL
, &undefined
);
692 } END_FOR_EACH_PTR(tmp
);
693 set_state(link_id
, sm
->name
, sm
->sym
, &undefined
);
696 static char *chunk_to_var_sym(struct expression
*expr
, struct symbol
**sym
)
698 expr
= strip_expr(expr
);
704 if (expr
->type
== EXPR_PREOP
&&
705 (expr
->op
== SPECIAL_INCREMENT
||
706 expr
->op
== SPECIAL_DECREMENT
))
707 expr
= strip_expr(expr
->unop
);
709 return expr_to_chunk_sym_vsl(expr
, sym
, NULL
);
712 static char *chunk_to_var(struct expression
*expr
)
714 return chunk_to_var_sym(expr
, NULL
);
717 static void save_link(struct expression
*expr
, char *link
)
722 expr
= strip_expr(expr
);
723 if (expr
->type
== EXPR_BINOP
) {
726 chunk
= chunk_to_var(expr
);
730 save_link(expr
->left
, link
);
731 save_link(expr
->right
, link
);
732 save_link_var_sym(chunk
, NULL
, link
);
736 var
= expr_to_var_sym(expr
, &sym
);
740 save_link_var_sym(var
, sym
, link
);
745 static int get_orig_comparison(struct stree
*pre_stree
, const char *left
, const char *right
)
747 struct smatch_state
*state
;
748 struct compare_data
*data
;
750 char state_name
[256];
752 if (strcmp(left
, right
) > 0) {
753 const char *tmp
= right
;
760 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left
, right
);
761 state
= get_state_stree(pre_stree
, compare_id
, state_name
, NULL
);
762 if (!state
|| !state
->data
)
766 return flip_comparison(data
->comparison
);
767 return data
->comparison
;
771 static int have_common_var_sym(struct var_sym_list
*left_vsl
, struct var_sym_list
*right_vsl
)
775 FOR_EACH_PTR(left_vsl
, tmp
) {
776 if (in_var_sym_list(right_vsl
, tmp
->var
, tmp
->sym
))
778 } END_FOR_EACH_PTR(tmp
);
784 * The idea here is that we take a comparison "a < b" and then we look at all
785 * the things which "b" is compared against "b < c" and we say that that implies
786 * a relationship "a < c".
788 * The names here about because the comparisons are organized like this
792 static void update_tf_links(struct stree
*pre_stree
,
793 const char *left_var
, struct var_sym_list
*left_vsl
,
794 int left_comparison
, int left_false_comparison
,
795 const char *mid_var
, struct var_sym_list
*mid_vsl
,
796 struct string_list
*links
)
798 struct smatch_state
*state
;
799 struct smatch_state
*true_state
, *false_state
;
800 struct compare_data
*data
;
801 const char *right_var
;
802 struct var_sym_list
*right_vsl
;
804 int right_comparison
;
806 int false_comparison
;
808 char state_name
[256];
811 FOR_EACH_PTR(links
, tmp
) {
812 state
= get_state_stree(pre_stree
, compare_id
, tmp
, NULL
);
813 if (!state
|| !state
->data
)
816 right_comparison
= data
->comparison
;
817 right_var
= data
->var2
;
818 right_vsl
= data
->vsl2
;
819 if (strcmp(mid_var
, right_var
) == 0) {
820 right_var
= data
->var1
;
821 right_vsl
= data
->vsl1
;
822 right_comparison
= flip_comparison(right_comparison
);
824 if (have_common_var_sym(left_vsl
, right_vsl
))
827 orig_comparison
= get_orig_comparison(pre_stree
, left_var
, right_var
);
829 true_comparison
= combine_comparisons(left_comparison
, right_comparison
);
830 false_comparison
= combine_comparisons(left_false_comparison
, right_comparison
);
832 true_comparison
= filter_comparison(orig_comparison
, true_comparison
);
833 false_comparison
= filter_comparison(orig_comparison
, false_comparison
);
835 if (strcmp(left_var
, right_var
) > 0) {
836 const char *tmp_var
= left_var
;
837 struct var_sym_list
*tmp_vsl
= left_vsl
;
839 left_var
= right_var
;
840 left_vsl
= right_vsl
;
843 true_comparison
= flip_comparison(true_comparison
);
844 false_comparison
= flip_comparison(false_comparison
);
847 if (!true_comparison
&& !false_comparison
)
851 true_state
= alloc_compare_state(left_var
, left_vsl
, true_comparison
, right_var
, right_vsl
);
854 if (false_comparison
)
855 false_state
= alloc_compare_state(left_var
, left_vsl
, false_comparison
, right_var
, right_vsl
);
859 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left_var
, right_var
);
860 set_true_false_states(compare_id
, state_name
, NULL
, true_state
, false_state
);
861 FOR_EACH_PTR(left_vsl
, vs
) {
862 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
863 } END_FOR_EACH_PTR(vs
);
864 FOR_EACH_PTR(right_vsl
, vs
) {
865 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
866 } END_FOR_EACH_PTR(vs
);
867 if (!vsl_to_sym(left_vsl
))
868 save_link_var_sym(left_var
, NULL
, state_name
);
869 if (!vsl_to_sym(right_vsl
))
870 save_link_var_sym(right_var
, NULL
, state_name
);
871 } END_FOR_EACH_PTR(tmp
);
874 static void update_tf_data(struct stree
*pre_stree
,
875 const char *left_name
, struct var_sym_list
*left_vsl
,
876 const char *right_name
, struct var_sym_list
*right_vsl
,
877 int true_comparison
, int false_comparison
)
879 struct smatch_state
*state
;
881 state
= get_state_stree(pre_stree
, link_id
, right_name
, vsl_to_sym(right_vsl
));
883 update_tf_links(pre_stree
, left_name
, left_vsl
, true_comparison
, false_comparison
, right_name
, right_vsl
, state
->data
);
885 state
= get_state_stree(pre_stree
, link_id
, left_name
, vsl_to_sym(left_vsl
));
887 update_tf_links(pre_stree
, right_name
, right_vsl
, flip_comparison(true_comparison
), flip_comparison(false_comparison
), left_name
, left_vsl
, state
->data
);
890 static void iter_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
892 if (sm
->state
!= &start
||
894 (mod_expr
->type
!= EXPR_PREOP
&& mod_expr
->type
!= EXPR_POSTOP
) ||
895 mod_expr
->op
!= SPECIAL_INCREMENT
)
896 set_state(inc_dec_id
, sm
->name
, sm
->sym
, &undefined
);
898 set_state(inc_dec_id
, sm
->name
, sm
->sym
, &incremented
);
901 static void handle_for_loops(struct expression
*expr
, char *state_name
, struct smatch_state
*false_state
)
904 char *iter_name
, *cap_name
;
905 struct symbol
*iter_sym
, *cap_sym
;
906 struct compare_data
*data
;
908 if (expr
->op
!= '<' && expr
->op
!= SPECIAL_UNSIGNED_LT
)
911 if (!__cur_stmt
|| !__prev_stmt
)
913 if (__cur_stmt
->type
!= STMT_ITERATOR
)
915 if (__cur_stmt
->iterator_pre_condition
!= expr
)
918 /* literals are handled in smatch_extra.c */
919 if (get_value(expr
->right
, &sval
))
922 /* First time checking the condition */
923 if (__prev_stmt
== __cur_stmt
->iterator_pre_statement
) {
924 if (!get_implied_value(expr
->left
, &sval
) ||
928 iter_name
= expr_to_var_sym(expr
->left
, &iter_sym
);
929 cap_name
= expr_to_var_sym(expr
->right
, &cap_sym
);
930 if (!iter_name
|| !cap_name
|| !iter_sym
|| !cap_sym
) {
931 free_string(iter_name
);
932 free_string(cap_name
);
936 set_state(inc_dec_id
, iter_name
, iter_sym
, &start
);
937 store_link(inc_dec_link_id
, cap_name
, cap_sym
, iter_name
, iter_sym
);
939 free_string(iter_name
);
940 free_string(cap_name
);
944 /* Second time checking the condtion */
945 if (__prev_stmt
!= __cur_stmt
->iterator_post_statement
)
948 if (get_state_expr(inc_dec_id
, expr
->left
) != &incremented
)
951 data
= false_state
->data
;
952 false_state
= alloc_compare_state(data
->var1
, data
->vsl1
, SPECIAL_EQUAL
, data
->var2
, data
->vsl2
);
954 set_true_false_states(compare_id
, state_name
, NULL
, NULL
, false_state
);
957 static void handle_comparison(struct expression
*left_expr
, int op
, struct expression
*right_expr
, char **_state_name
, struct smatch_state
**_false_state
)
961 struct symbol
*left_sym
, *right_sym
;
962 struct var_sym_list
*left_vsl
, *right_vsl
;
965 struct smatch_state
*true_state
, *false_state
;
966 static char state_name
[256];
967 struct stree
*pre_stree
;
970 false_op
= negate_comparison(op
);
972 if (op
== SPECIAL_UNSIGNED_LT
&&
973 get_implied_value(left_expr
, &sval
) &&
975 false_op
= SPECIAL_EQUAL
;
977 if (op
== SPECIAL_UNSIGNED_GT
&&
978 get_implied_value(right_expr
, &sval
) &&
980 false_op
= SPECIAL_EQUAL
;
982 left
= chunk_to_var_sym(left_expr
, &left_sym
);
985 left_vsl
= expr_to_vsl(left_expr
);
986 right
= chunk_to_var_sym(right_expr
, &right_sym
);
989 right_vsl
= expr_to_vsl(right_expr
);
991 if (strcmp(left
, right
) > 0) {
992 struct symbol
*tmp_sym
= left_sym
;
993 char *tmp_name
= left
;
994 struct var_sym_list
*tmp_vsl
= left_vsl
;
995 struct expression
*tmp_expr
= left_expr
;
998 left_sym
= right_sym
;
999 left_vsl
= right_vsl
;
1000 left_expr
= right_expr
;
1002 right_sym
= tmp_sym
;
1003 right_vsl
= tmp_vsl
;
1004 right_expr
= tmp_expr
;
1005 op
= flip_comparison(op
);
1006 false_op
= flip_comparison(false_op
);
1009 orig_comparison
= get_comparison_strings(left
, right
);
1010 op
= filter_comparison(orig_comparison
, op
);
1011 false_op
= filter_comparison(orig_comparison
, false_op
);
1013 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left
, right
);
1014 true_state
= alloc_compare_state(left
, left_vsl
, op
, right
, right_vsl
);
1015 false_state
= alloc_compare_state(left
, left_vsl
, false_op
, right
, right_vsl
);
1017 pre_stree
= clone_stree(__get_cur_stree());
1018 update_tf_data(pre_stree
, left
, left_vsl
, right
, right_vsl
, op
, false_op
);
1019 free_stree(&pre_stree
);
1021 set_true_false_states(compare_id
, state_name
, NULL
, true_state
, false_state
);
1022 __compare_param_limit_hook(left_expr
, right_expr
, state_name
, true_state
, false_state
);
1023 save_link(left_expr
, state_name
);
1024 save_link(right_expr
, state_name
);
1027 *_false_state
= false_state
;
1029 *_state_name
= state_name
;
1036 void __comparison_match_condition(struct expression
*expr
)
1038 struct expression
*left
, *right
, *new_left
, *new_right
;
1039 struct smatch_state
*false_state
= NULL
;
1040 char *state_name
= NULL
;
1042 if (expr
->type
!= EXPR_COMPARE
)
1045 handle_comparison(expr
->left
, expr
->op
, expr
->right
, &state_name
, &false_state
);
1046 if (false_state
&& state_name
)
1047 handle_for_loops(expr
, state_name
, false_state
);
1050 left
= strip_parens(expr
->left
);
1051 right
= strip_parens(expr
->right
);
1053 if (left
->type
== EXPR_BINOP
&& left
->op
== '+') {
1054 new_left
= left
->left
;
1055 new_right
= binop_expression(right
, '-', left
->right
);
1056 handle_comparison(new_left
, expr
->op
, new_right
, NULL
, NULL
);
1058 new_left
= left
->right
;
1059 new_right
= binop_expression(right
, '-', left
->left
);
1060 handle_comparison(new_left
, expr
->op
, new_right
, NULL
, NULL
);
1064 static void add_comparison_var_sym(const char *left_name
,
1065 struct var_sym_list
*left_vsl
,
1067 const char *right_name
, struct var_sym_list
*right_vsl
)
1069 struct smatch_state
*state
;
1071 char state_name
[256];
1073 if (strcmp(left_name
, right_name
) > 0) {
1074 const char *tmp_name
= left_name
;
1075 struct var_sym_list
*tmp_vsl
= left_vsl
;
1077 left_name
= right_name
;
1078 left_vsl
= right_vsl
;
1079 right_name
= tmp_name
;
1080 right_vsl
= tmp_vsl
;
1081 comparison
= flip_comparison(comparison
);
1083 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left_name
, right_name
);
1084 state
= alloc_compare_state(left_name
, left_vsl
, comparison
, right_name
, right_vsl
);
1086 set_state(compare_id
, state_name
, NULL
, state
);
1088 FOR_EACH_PTR(left_vsl
, vs
) {
1089 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
1090 } END_FOR_EACH_PTR(vs
);
1091 FOR_EACH_PTR(right_vsl
, vs
) {
1092 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
1093 } END_FOR_EACH_PTR(vs
);
1096 static void add_comparison(struct expression
*left
, int comparison
, struct expression
*right
)
1098 char *left_name
= NULL
;
1099 char *right_name
= NULL
;
1100 struct symbol
*left_sym
, *right_sym
;
1101 struct var_sym_list
*left_vsl
, *right_vsl
;
1102 struct smatch_state
*state
;
1103 char state_name
[256];
1105 left_name
= chunk_to_var_sym(left
, &left_sym
);
1108 left_vsl
= expr_to_vsl(left
);
1109 right_name
= chunk_to_var_sym(right
, &right_sym
);
1112 right_vsl
= expr_to_vsl(right
);
1114 if (strcmp(left_name
, right_name
) > 0) {
1115 struct symbol
*tmp_sym
= left_sym
;
1116 char *tmp_name
= left_name
;
1117 struct var_sym_list
*tmp_vsl
= left_vsl
;
1119 left_name
= right_name
;
1120 left_sym
= right_sym
;
1121 left_vsl
= right_vsl
;
1122 right_name
= tmp_name
;
1123 right_sym
= tmp_sym
;
1124 right_vsl
= tmp_vsl
;
1125 comparison
= flip_comparison(comparison
);
1127 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left_name
, right_name
);
1128 state
= alloc_compare_state(left_name
, left_vsl
, comparison
, right_name
, right_vsl
);
1130 set_state(compare_id
, state_name
, NULL
, state
);
1131 save_link(left
, state_name
);
1132 save_link(right
, state_name
);
1135 free_string(left_name
);
1136 free_string(right_name
);
1139 static void match_assign_add(struct expression
*expr
)
1141 struct expression
*right
;
1142 struct expression
*r_left
, *r_right
;
1143 sval_t left_tmp
, right_tmp
;
1145 right
= strip_expr(expr
->right
);
1146 r_left
= strip_expr(right
->left
);
1147 r_right
= strip_expr(right
->right
);
1149 get_absolute_min(r_left
, &left_tmp
);
1150 get_absolute_min(r_right
, &right_tmp
);
1152 if (left_tmp
.value
> 0)
1153 add_comparison(expr
->left
, '>', r_right
);
1154 else if (left_tmp
.value
== 0)
1155 add_comparison(expr
->left
, SPECIAL_GTE
, r_right
);
1157 if (right_tmp
.value
> 0)
1158 add_comparison(expr
->left
, '>', r_left
);
1159 else if (right_tmp
.value
== 0)
1160 add_comparison(expr
->left
, SPECIAL_GTE
, r_left
);
1163 static void match_assign_sub(struct expression
*expr
)
1165 struct expression
*right
;
1166 struct expression
*r_left
, *r_right
;
1170 right
= strip_expr(expr
->right
);
1171 r_left
= strip_expr(right
->left
);
1172 r_right
= strip_expr(right
->right
);
1174 if (get_absolute_min(r_right
, &min
) && sval_is_negative(min
))
1177 comparison
= get_comparison(r_left
, r_right
);
1179 switch (comparison
) {
1182 if (implied_not_equal(r_right
, 0))
1183 add_comparison(expr
->left
, '>', r_left
);
1185 add_comparison(expr
->left
, SPECIAL_GTE
, r_left
);
1190 static void match_assign_divide(struct expression
*expr
)
1192 struct expression
*right
;
1193 struct expression
*r_left
, *r_right
;
1196 right
= strip_expr(expr
->right
);
1197 r_left
= strip_expr(right
->left
);
1198 r_right
= strip_expr(right
->right
);
1199 if (!get_implied_min(r_right
, &min
) || min
.value
<= 1)
1202 add_comparison(expr
->left
, '<', r_left
);
1205 static void match_binop_assign(struct expression
*expr
)
1207 struct expression
*right
;
1209 right
= strip_expr(expr
->right
);
1210 if (right
->op
== '+')
1211 match_assign_add(expr
);
1212 if (right
->op
== '-')
1213 match_assign_sub(expr
);
1214 if (right
->op
== '/')
1215 match_assign_divide(expr
);
1218 static void copy_comparisons(struct expression
*left
, struct expression
*right
)
1220 struct string_list
*links
;
1221 struct smatch_state
*state
;
1222 struct compare_data
*data
;
1223 struct symbol
*left_sym
, *right_sym
;
1224 char *left_var
= NULL
;
1225 char *right_var
= NULL
;
1226 struct var_sym_list
*left_vsl
;
1228 struct var_sym_list
*vsl
;
1232 left_var
= chunk_to_var_sym(left
, &left_sym
);
1235 left_vsl
= expr_to_vsl(left
);
1236 right_var
= chunk_to_var_sym(right
, &right_sym
);
1240 state
= get_state(link_id
, right_var
, right_sym
);
1243 links
= state
->data
;
1245 FOR_EACH_PTR(links
, tmp
) {
1246 state
= get_state(compare_id
, tmp
, NULL
);
1247 if (!state
|| !state
->data
)
1250 comparison
= data
->comparison
;
1253 if (strcmp(var
, right_var
) == 0) {
1256 comparison
= flip_comparison(comparison
);
1258 add_comparison_var_sym(left_var
, left_vsl
, comparison
, var
, vsl
);
1259 } END_FOR_EACH_PTR(tmp
);
1262 free_string(right_var
);
1265 static void match_assign(struct expression
*expr
)
1267 struct expression
*right
;
1269 if (expr
->op
!= '=')
1271 if (__in_fake_assign
|| outside_of_function())
1274 if (is_struct(expr
->left
))
1277 copy_comparisons(expr
->left
, expr
->right
);
1278 add_comparison(expr
->left
, SPECIAL_EQUAL
, expr
->right
);
1280 right
= strip_expr(expr
->right
);
1281 if (right
->type
== EXPR_BINOP
)
1282 match_binop_assign(expr
);
1285 int get_comparison_strings(const char *one
, const char *two
)
1288 struct smatch_state
*state
;
1292 if (strcmp(one
, two
) == 0)
1293 return SPECIAL_EQUAL
;
1295 if (strcmp(one
, two
) > 0) {
1296 const char *tmp
= one
;
1303 snprintf(buf
, sizeof(buf
), "%s vs %s", one
, two
);
1304 state
= get_state(compare_id
, buf
, NULL
);
1306 ret
= state_to_comparison(state
);
1309 ret
= flip_comparison(ret
);
1314 int get_comparison(struct expression
*a
, struct expression
*b
)
1320 one
= chunk_to_var(a
);
1323 two
= chunk_to_var(b
);
1327 ret
= get_comparison_strings(one
, two
);
1334 int possible_comparison(struct expression
*a
, int comparison
, struct expression
*b
)
1340 struct sm_state
*sm
;
1343 one
= chunk_to_var(a
);
1346 two
= chunk_to_var(b
);
1351 if (strcmp(one
, two
) == 0 && comparison
== SPECIAL_EQUAL
) {
1356 if (strcmp(one
, two
) > 0) {
1361 comparison
= flip_comparison(comparison
);
1364 snprintf(buf
, sizeof(buf
), "%s vs %s", one
, two
);
1365 sm
= get_sm_state(compare_id
, buf
, NULL
);
1369 FOR_EACH_PTR(sm
->possible
, sm
) {
1370 if (!sm
->state
->data
)
1372 saved
= ((struct compare_data
*)sm
->state
->data
)->comparison
;
1373 if (saved
== comparison
)
1375 if (comparison
== SPECIAL_EQUAL
&&
1376 (saved
== SPECIAL_LTE
||
1377 saved
== SPECIAL_GTE
||
1378 saved
== SPECIAL_UNSIGNED_LTE
||
1379 saved
== SPECIAL_UNSIGNED_GTE
))
1383 } END_FOR_EACH_PTR(sm
);
1392 struct state_list
*get_all_comparisons(struct expression
*expr
)
1394 struct smatch_state
*state
;
1395 struct string_list
*links
;
1396 struct state_list
*ret
= NULL
;
1397 struct sm_state
*sm
;
1400 state
= get_state_expr(link_id
, expr
);
1403 links
= state
->data
;
1405 FOR_EACH_PTR(links
, tmp
) {
1406 sm
= get_sm_state(compare_id
, tmp
, NULL
);
1409 // FIXME have to compare name with vsl
1410 add_ptr_list(&ret
, sm
);
1411 } END_FOR_EACH_PTR(tmp
);
1416 struct state_list
*get_all_possible_equal_comparisons(struct expression
*expr
)
1418 struct smatch_state
*state
;
1419 struct string_list
*links
;
1420 struct state_list
*ret
= NULL
;
1421 struct sm_state
*sm
;
1424 state
= get_state_expr(link_id
, expr
);
1427 links
= state
->data
;
1429 FOR_EACH_PTR(links
, tmp
) {
1430 sm
= get_sm_state(compare_id
, tmp
, NULL
);
1433 if (!strchr(sm
->state
->name
, '='))
1435 if (strcmp(sm
->state
->name
, "!=") == 0)
1437 add_ptr_list(&ret
, sm
);
1438 } END_FOR_EACH_PTR(tmp
);
1443 static void update_links_from_call(struct expression
*left
,
1445 struct expression
*right
)
1447 struct string_list
*links
;
1448 struct smatch_state
*state
;
1449 struct compare_data
*data
;
1450 struct symbol
*left_sym
, *right_sym
;
1451 char *left_var
= NULL
;
1452 char *right_var
= NULL
;
1453 struct var_sym_list
*left_vsl
;
1455 struct var_sym_list
*vsl
;
1459 left_var
= chunk_to_var_sym(left
, &left_sym
);
1462 left_vsl
= expr_to_vsl(left
);
1463 right_var
= chunk_to_var_sym(right
, &right_sym
);
1467 state
= get_state(link_id
, right_var
, right_sym
);
1470 links
= state
->data
;
1472 FOR_EACH_PTR(links
, tmp
) {
1473 state
= get_state(compare_id
, tmp
, NULL
);
1474 if (!state
|| !state
->data
)
1477 comparison
= data
->comparison
;
1480 if (strcmp(var
, right_var
) == 0) {
1483 comparison
= flip_comparison(comparison
);
1485 comparison
= combine_comparisons(left_compare
, comparison
);
1488 add_comparison_var_sym(left_var
, left_vsl
, comparison
, var
, vsl
);
1489 } END_FOR_EACH_PTR(tmp
);
1492 free_string(right_var
);
1495 void __add_comparison_info(struct expression
*expr
, struct expression
*call
, const char *range
)
1497 struct expression
*arg
;
1499 const char *c
= range
;
1501 if (!str_to_comparison_arg(c
, call
, &comparison
, &arg
))
1503 update_links_from_call(expr
, comparison
, arg
);
1504 add_comparison(expr
, comparison
, arg
);
1507 static char *range_comparison_to_param_helper(struct expression
*expr
, char starts_with
, int ignore
)
1509 struct symbol
*param
;
1512 char *ret_str
= NULL
;
1516 var
= chunk_to_var(expr
);
1521 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
1527 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
1528 compare
= get_comparison_strings(var
, buf
);
1531 if (show_special(compare
)[0] != starts_with
)
1533 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(compare
), i
);
1534 ret_str
= alloc_sname(buf
);
1536 } END_FOR_EACH_PTR(param
);
1543 char *name_sym_to_param_comparison(const char *name
, struct symbol
*sym
)
1545 struct symbol
*param
;
1551 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
1555 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
1556 compare
= get_comparison_strings(name
, buf
);
1559 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(compare
), i
);
1560 return alloc_sname(buf
);
1561 } END_FOR_EACH_PTR(param
);
1566 char *expr_equal_to_param(struct expression
*expr
, int ignore
)
1568 return range_comparison_to_param_helper(expr
, '=', ignore
);
1571 char *expr_lte_to_param(struct expression
*expr
, int ignore
)
1573 return range_comparison_to_param_helper(expr
, '<', ignore
);
1576 char *expr_param_comparison(struct expression
*expr
, int ignore
)
1578 struct symbol
*param
;
1581 char *ret_str
= NULL
;
1585 var
= chunk_to_var(expr
);
1590 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
1596 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
1597 compare
= get_comparison_strings(var
, buf
);
1600 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(compare
), i
);
1601 ret_str
= alloc_sname(buf
);
1603 } END_FOR_EACH_PTR(param
);
1610 char *get_printed_param_name(struct expression
*call
, const char *param_name
, struct symbol
*param_sym
)
1612 struct expression
*arg
;
1615 static char buf
[256];
1620 FOR_EACH_PTR(call
->args
, arg
) {
1623 name
= expr_to_var_sym(arg
, &sym
);
1626 if (sym
!= param_sym
)
1630 if (strncmp(name
, param_name
, len
) != 0)
1632 if (param_name
[len
] == '\0') {
1633 snprintf(buf
, sizeof(buf
), "$%d", i
);
1636 if (param_name
[len
] != '-')
1638 snprintf(buf
, sizeof(buf
), "$%d%s", i
, param_name
+ len
);
1640 } END_FOR_EACH_PTR(arg
);
1645 static void match_call_info(struct expression
*expr
)
1647 struct expression
*arg
;
1648 struct smatch_state
*state
;
1649 struct sm_state
*sm
;
1650 struct compare_data
*data
;
1652 struct string_list
*links
;
1654 const char *right_name
;
1660 FOR_EACH_PTR(expr
->args
, arg
) {
1663 state
= get_state_expr(link_id
, arg
);
1667 links
= state
->data
;
1668 FOR_EACH_PTR(links
, link
) {
1669 struct var_sym_list
*right_vsl
;
1670 struct var_sym
*right_vs
;
1673 if (strstr(link
, " orig"))
1675 sm
= get_sm_state(compare_id
, link
, NULL
);
1678 data
= sm
->state
->data
;
1679 if (!data
|| !data
->comparison
)
1681 arg_name
= expr_to_var(arg
);
1686 if (strcmp(data
->var1
, arg_name
) == 0) {
1687 comparison
= data
->comparison
;
1688 right_name
= data
->var2
;
1689 right_vsl
= data
->vsl2
;
1690 } else if (strcmp(data
->var2
, arg_name
) == 0) {
1691 comparison
= flip_comparison(data
->comparison
);
1692 right_name
= data
->var1
;
1693 right_vsl
= data
->vsl1
;
1695 if (!right_vsl
|| ptr_list_size((struct ptr_list
*)right_vsl
) != 1)
1698 right_vs
= first_ptr_list((struct ptr_list
*)right_vsl
);
1699 if (strcmp(right_vs
->var
, right_name
) != 0)
1701 right_name
= get_printed_param_name(expr
, right_vs
->var
, right_vs
->sym
);
1704 snprintf(info_buf
, sizeof(info_buf
), "%s %s", show_special(comparison
), right_name
);
1705 sql_insert_caller_info(expr
, PARAM_COMPARE
, i
, "$", info_buf
);
1708 free_string(arg_name
);
1709 } END_FOR_EACH_PTR(link
);
1710 } END_FOR_EACH_PTR(arg
);
1713 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*link_sm
)
1715 struct sm_state
*compare_sm
;
1716 struct string_list
*links
;
1718 struct compare_data
*data
;
1719 struct var_sym
*left
, *right
;
1720 static char info_buf
[256];
1721 const char *right_name
;
1723 if (strstr(printed_name
, " orig"))
1726 links
= link_sm
->state
->data
;
1727 FOR_EACH_PTR(links
, link
) {
1728 compare_sm
= get_sm_state(compare_id
, link
, NULL
);
1731 data
= compare_sm
->state
->data
;
1732 if (!data
|| !data
->comparison
)
1735 if (ptr_list_size((struct ptr_list
*)data
->vsl1
) != 1 ||
1736 ptr_list_size((struct ptr_list
*)data
->vsl2
) != 1)
1738 left
= first_ptr_list((struct ptr_list
*)data
->vsl1
);
1739 right
= first_ptr_list((struct ptr_list
*)data
->vsl2
);
1740 if (left
->sym
== right
->sym
&&
1741 strcmp(left
->var
, right
->var
) == 0)
1744 * Both parameters link to this comparison so only
1745 * record the first one.
1747 if (left
->sym
!= link_sm
->sym
||
1748 strcmp(left
->var
, link_sm
->name
) != 0)
1751 right_name
= get_printed_param_name(call
, right
->var
, right
->sym
);
1754 snprintf(info_buf
, sizeof(info_buf
), "%s %s", show_special(data
->comparison
), right_name
);
1755 sql_insert_caller_info(call
, PARAM_COMPARE
, param
, printed_name
, info_buf
);
1756 } END_FOR_EACH_PTR(link
);
1759 static void print_return_value_comparison(int return_id
, char *return_ranges
, struct expression
*expr
)
1762 const char *tmp_name
;
1768 * TODO: This only prints == comparisons. That's probably the most
1769 * useful comparison because == max has lots of implications. But it
1770 * would be good to capture the rest as well.
1772 * This information is already in the DB but it's in the parameter math
1773 * bits and it's awkward to use it. This is is the simpler, possibly
1774 * cleaner way, but not necessarily the best, I don't know.
1779 name
= expr_to_var_sym(expr
, &sym
);
1783 param
= get_param_num_from_sym(sym
);
1786 if (param_was_set_var_sym(name
, sym
))
1789 tmp_name
= get_param_name_var_sym(name
, sym
);
1793 snprintf(info_buf
, sizeof(info_buf
), "== $%d%s", param
, tmp_name
+ 1);
1794 sql_insert_return_states(return_id
, return_ranges
,
1795 PARAM_COMPARE
, -1, "$", info_buf
);
1800 static void print_return_comparison(int return_id
, char *return_ranges
, struct expression
*expr
)
1802 struct sm_state
*tmp
;
1803 struct string_list
*links
;
1805 struct sm_state
*sm
;
1806 struct compare_data
*data
;
1807 struct var_sym
*left
, *right
;
1808 int left_param
, right_param
;
1810 char right_buf
[256];
1812 const char *tmp_name
;
1814 print_return_value_comparison(return_id
, return_ranges
, expr
);
1816 FOR_EACH_MY_SM(link_id
, __get_cur_stree(), tmp
) {
1817 if (get_param_num_from_sym(tmp
->sym
) < 0)
1819 links
= tmp
->state
->data
;
1820 FOR_EACH_PTR(links
, link
) {
1821 sm
= get_sm_state(compare_id
, link
, NULL
);
1824 data
= sm
->state
->data
;
1825 if (!data
|| !data
->comparison
)
1827 if (ptr_list_size((struct ptr_list
*)data
->vsl1
) != 1 ||
1828 ptr_list_size((struct ptr_list
*)data
->vsl2
) != 1)
1830 left
= first_ptr_list((struct ptr_list
*)data
->vsl1
);
1831 right
= first_ptr_list((struct ptr_list
*)data
->vsl2
);
1832 if (left
->sym
== right
->sym
&&
1833 strcmp(left
->var
, right
->var
) == 0)
1836 * Both parameters link to this comparison so only
1837 * record the first one.
1839 if (left
->sym
!= tmp
->sym
||
1840 strcmp(left
->var
, tmp
->name
) != 0)
1843 if (strstr(right
->var
, " orig"))
1846 left_param
= get_param_num_from_sym(left
->sym
);
1847 right_param
= get_param_num_from_sym(right
->sym
);
1848 if (left_param
< 0 || right_param
< 0)
1851 tmp_name
= get_param_name_var_sym(left
->var
, left
->sym
);
1854 snprintf(left_buf
, sizeof(left_buf
), "%s", tmp_name
);
1856 tmp_name
= get_param_name_var_sym(right
->var
, right
->sym
);
1857 if (!tmp_name
|| tmp_name
[0] != '$')
1859 snprintf(right_buf
, sizeof(right_buf
), "$%d%s", right_param
, tmp_name
+ 1);
1862 * FIXME: this should reject $ type variables (as
1863 * opposed to $->foo type). Those should come from
1864 * smatch_param_compare_limit.c.
1867 snprintf(info_buf
, sizeof(info_buf
), "%s %s", show_special(data
->comparison
), right_buf
);
1868 sql_insert_return_states(return_id
, return_ranges
,
1869 PARAM_COMPARE
, left_param
, left_buf
, info_buf
);
1870 } END_FOR_EACH_PTR(link
);
1872 } END_FOR_EACH_SM(tmp
);
1875 static int parse_comparison(char **value
, int *op
)
1883 if (**value
== '=') {
1891 *op
= SPECIAL_EQUAL
;
1896 *op
= SPECIAL_NOTEQUAL
;
1900 if (**value
== '=') {
1909 if (**value
!= ' ') {
1910 sm_msg("internal error parsing comparison. %s", *value
);
1918 static int split_op_param_key(char *value
, int *op
, int *param
, char **key
)
1920 static char buf
[256];
1923 if (!parse_comparison(&value
, op
))
1926 snprintf(buf
, sizeof(buf
), value
);
1933 if (*param
< 0 || *param
> 99)
1945 static void db_return_comparison(struct expression
*expr
, int left_param
, char *key
, char *value
)
1947 struct expression
*left_arg
, *right_arg
;
1948 char *left_name
= NULL
;
1949 struct symbol
*left_sym
;
1950 char *right_name
= NULL
;
1951 struct symbol
*right_sym
;
1955 struct var_sym_list
*left_vsl
= NULL
, *right_vsl
= NULL
;
1957 if (left_param
== -1) {
1958 if (expr
->type
!= EXPR_ASSIGNMENT
)
1960 left_arg
= strip_expr(expr
->left
);
1962 while (expr
->type
== EXPR_ASSIGNMENT
)
1963 expr
= strip_expr(expr
->right
);
1964 if (expr
->type
!= EXPR_CALL
)
1967 while (expr
->type
== EXPR_ASSIGNMENT
)
1968 expr
= strip_expr(expr
->right
);
1969 if (expr
->type
!= EXPR_CALL
)
1972 left_arg
= get_argument_from_call_expr(expr
->args
, left_param
);
1977 if (!split_op_param_key(value
, &op
, &right_param
, &right_key
))
1980 right_arg
= get_argument_from_call_expr(expr
->args
, right_param
);
1984 left_name
= get_variable_from_key(left_arg
, key
, &left_sym
);
1985 if (!left_name
|| !left_sym
)
1988 right_name
= get_variable_from_key(right_arg
, right_key
, &right_sym
);
1989 if (!right_name
|| !right_sym
)
1992 add_var_sym(&left_vsl
, left_name
, left_sym
);
1993 add_var_sym(&right_vsl
, right_name
, right_sym
);
1995 add_comparison_var_sym(left_name
, left_vsl
, op
, right_name
, right_vsl
);
1998 free_string(left_name
);
1999 free_string(right_name
);
2002 int param_compare_limit_is_impossible(struct expression
*expr
, int left_param
, char *left_key
, char *value
)
2004 struct smatch_state
*state
;
2005 char *left_name
= NULL
;
2006 char *right_name
= NULL
;
2007 struct symbol
*left_sym
, *right_sym
;
2008 struct expression
*left_arg
, *right_arg
;
2015 while (expr
->type
== EXPR_ASSIGNMENT
)
2016 expr
= strip_expr(expr
->right
);
2017 if (expr
->type
!= EXPR_CALL
)
2020 if (!split_op_param_key(value
, &op
, &right_param
, &right_key
))
2023 left_arg
= get_argument_from_call_expr(expr
->args
, left_param
);
2027 right_arg
= get_argument_from_call_expr(expr
->args
, right_param
);
2031 left_name
= get_variable_from_key(left_arg
, left_key
, &left_sym
);
2032 right_name
= get_variable_from_key(right_arg
, right_key
, &right_sym
);
2033 if (!left_name
|| !right_name
)
2036 snprintf(buf
, sizeof(buf
), "%s vs %s", left_name
, right_name
);
2037 state
= get_state(compare_id
, buf
, NULL
);
2040 if (!state_to_comparison(state
))
2043 if (!filter_comparison(state_to_comparison(state
), op
))
2046 free_string(left_name
);
2047 free_string(right_name
);
2051 static void free_data(struct symbol
*sym
)
2055 clear_compare_data_alloc();
2058 void register_comparison(int id
)
2061 add_hook(&save_start_states
, AFTER_DEF_HOOK
);
2062 add_unmatched_state_hook(compare_id
, unmatched_comparison
);
2063 add_merge_hook(compare_id
, &merge_compare_states
);
2064 add_hook(&free_data
, AFTER_FUNC_HOOK
);
2065 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
2066 add_split_return_callback(&print_return_comparison
);
2068 select_return_states_hook(PARAM_COMPARE
, &db_return_comparison
);
2071 void register_comparison_late(int id
)
2073 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
2076 void register_comparison_links(int id
)
2079 add_merge_hook(link_id
, &merge_links
);
2080 add_modification_hook(link_id
, &match_modify
);
2082 add_member_info_callback(link_id
, struct_member_callback
);
2085 void register_comparison_inc_dec(int id
)
2088 add_modification_hook_late(inc_dec_id
, &iter_modify
);
2091 void register_comparison_inc_dec_links(int id
)
2093 inc_dec_link_id
= id
;
2094 set_up_link_functions(inc_dec_id
, inc_dec_link_id
);
2097 static void filter_by_sm(struct sm_state
*sm
, int op
,
2098 struct state_list
**true_stack
,
2099 struct state_list
**false_stack
)
2101 struct compare_data
*data
;
2107 data
= sm
->state
->data
;
2110 filter_by_sm(sm
->left
, op
, true_stack
, false_stack
);
2111 filter_by_sm(sm
->right
, op
, true_stack
, false_stack
);
2116 if (data
->comparison
&&
2117 data
->comparison
== filter_comparison(data
->comparison
, op
))
2120 if (data
->comparison
&&
2121 data
->comparison
== filter_comparison(data
->comparison
, negate_comparison(op
)))
2125 add_ptr_list(true_stack
, sm
);
2127 add_ptr_list(false_stack
, sm
);
2130 filter_by_sm(sm
->left
, op
, true_stack
, false_stack
);
2131 filter_by_sm(sm
->right
, op
, true_stack
, false_stack
);
2135 struct sm_state
*comparison_implication_hook(struct expression
*expr
,
2136 struct state_list
**true_stack
,
2137 struct state_list
**false_stack
)
2139 struct sm_state
*sm
;
2142 static char buf
[256];
2144 if (expr
->type
!= EXPR_COMPARE
)
2149 left
= expr_to_var(expr
->left
);
2150 right
= expr_to_var(expr
->right
);
2151 if (!left
|| !right
) {
2157 if (strcmp(left
, right
) > 0) {
2162 op
= flip_comparison(op
);
2165 snprintf(buf
, sizeof(buf
), "%s vs %s", left
, right
);
2166 sm
= get_sm_state(compare_id
, buf
, NULL
);
2172 filter_by_sm(sm
, op
, true_stack
, false_stack
);
2173 if (!*true_stack
&& !*false_stack
)