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 if (expr
->type
== EXPR_CALL
) {
712 snprintf(buf
, sizeof(buf
), "return %p", expr
);
713 return alloc_string(buf
);
716 return expr_to_chunk_sym_vsl(expr
, sym
, NULL
);
719 static char *chunk_to_var(struct expression
*expr
)
721 return chunk_to_var_sym(expr
, NULL
);
724 static struct smatch_state
*get_state_chunk(int owner
, struct expression
*expr
)
728 struct smatch_state
*ret
;
730 name
= chunk_to_var_sym(expr
, &sym
);
734 ret
= get_state(owner
, name
, sym
);
739 static void save_link(struct expression
*expr
, char *link
)
744 expr
= strip_expr(expr
);
745 if (expr
->type
== EXPR_BINOP
) {
748 chunk
= chunk_to_var(expr
);
752 save_link(expr
->left
, link
);
753 save_link(expr
->right
, link
);
754 save_link_var_sym(chunk
, NULL
, link
);
758 var
= chunk_to_var_sym(expr
, &sym
);
762 save_link_var_sym(var
, sym
, link
);
766 static int get_orig_comparison(struct stree
*pre_stree
, const char *left
, const char *right
)
768 struct smatch_state
*state
;
769 struct compare_data
*data
;
771 char state_name
[256];
773 if (strcmp(left
, right
) > 0) {
774 const char *tmp
= right
;
781 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left
, right
);
782 state
= get_state_stree(pre_stree
, compare_id
, state_name
, NULL
);
783 if (!state
|| !state
->data
)
787 return flip_comparison(data
->comparison
);
788 return data
->comparison
;
792 static int have_common_var_sym(struct var_sym_list
*left_vsl
, struct var_sym_list
*right_vsl
)
796 FOR_EACH_PTR(left_vsl
, tmp
) {
797 if (in_var_sym_list(right_vsl
, tmp
->var
, tmp
->sym
))
799 } END_FOR_EACH_PTR(tmp
);
805 * The idea here is that we take a comparison "a < b" and then we look at all
806 * the things which "b" is compared against "b < c" and we say that that implies
807 * a relationship "a < c".
809 * The names here about because the comparisons are organized like this
813 static void update_tf_links(struct stree
*pre_stree
,
814 const char *left_var
, struct var_sym_list
*left_vsl
,
815 int left_comparison
, int left_false_comparison
,
816 const char *mid_var
, struct var_sym_list
*mid_vsl
,
817 struct string_list
*links
)
819 struct smatch_state
*state
;
820 struct smatch_state
*true_state
, *false_state
;
821 struct compare_data
*data
;
822 const char *right_var
;
823 struct var_sym_list
*right_vsl
;
825 int right_comparison
;
827 int false_comparison
;
829 char state_name
[256];
832 FOR_EACH_PTR(links
, tmp
) {
833 state
= get_state_stree(pre_stree
, compare_id
, tmp
, NULL
);
834 if (!state
|| !state
->data
)
837 right_comparison
= data
->comparison
;
838 right_var
= data
->var2
;
839 right_vsl
= data
->vsl2
;
840 if (strcmp(mid_var
, right_var
) == 0) {
841 right_var
= data
->var1
;
842 right_vsl
= data
->vsl1
;
843 right_comparison
= flip_comparison(right_comparison
);
845 if (have_common_var_sym(left_vsl
, right_vsl
))
848 orig_comparison
= get_orig_comparison(pre_stree
, left_var
, right_var
);
850 true_comparison
= combine_comparisons(left_comparison
, right_comparison
);
851 false_comparison
= combine_comparisons(left_false_comparison
, right_comparison
);
853 true_comparison
= filter_comparison(orig_comparison
, true_comparison
);
854 false_comparison
= filter_comparison(orig_comparison
, false_comparison
);
856 if (strcmp(left_var
, right_var
) > 0) {
857 const char *tmp_var
= left_var
;
858 struct var_sym_list
*tmp_vsl
= left_vsl
;
860 left_var
= right_var
;
861 left_vsl
= right_vsl
;
864 true_comparison
= flip_comparison(true_comparison
);
865 false_comparison
= flip_comparison(false_comparison
);
868 if (!true_comparison
&& !false_comparison
)
872 true_state
= alloc_compare_state(left_var
, left_vsl
, true_comparison
, right_var
, right_vsl
);
875 if (false_comparison
)
876 false_state
= alloc_compare_state(left_var
, left_vsl
, false_comparison
, right_var
, right_vsl
);
880 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left_var
, right_var
);
881 set_true_false_states(compare_id
, state_name
, NULL
, true_state
, false_state
);
882 FOR_EACH_PTR(left_vsl
, vs
) {
883 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
884 } END_FOR_EACH_PTR(vs
);
885 FOR_EACH_PTR(right_vsl
, vs
) {
886 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
887 } END_FOR_EACH_PTR(vs
);
888 if (!vsl_to_sym(left_vsl
))
889 save_link_var_sym(left_var
, NULL
, state_name
);
890 if (!vsl_to_sym(right_vsl
))
891 save_link_var_sym(right_var
, NULL
, state_name
);
892 } END_FOR_EACH_PTR(tmp
);
895 static void update_tf_data(struct stree
*pre_stree
,
896 const char *left_name
, struct var_sym_list
*left_vsl
,
897 const char *right_name
, struct var_sym_list
*right_vsl
,
898 int true_comparison
, int false_comparison
)
900 struct smatch_state
*state
;
902 state
= get_state_stree(pre_stree
, link_id
, right_name
, vsl_to_sym(right_vsl
));
904 update_tf_links(pre_stree
, left_name
, left_vsl
, true_comparison
, false_comparison
, right_name
, right_vsl
, state
->data
);
906 state
= get_state_stree(pre_stree
, link_id
, left_name
, vsl_to_sym(left_vsl
));
908 update_tf_links(pre_stree
, right_name
, right_vsl
, flip_comparison(true_comparison
), flip_comparison(false_comparison
), left_name
, left_vsl
, state
->data
);
911 static void iter_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
913 if (sm
->state
!= &start
||
915 (mod_expr
->type
!= EXPR_PREOP
&& mod_expr
->type
!= EXPR_POSTOP
) ||
916 mod_expr
->op
!= SPECIAL_INCREMENT
)
917 set_state(inc_dec_id
, sm
->name
, sm
->sym
, &undefined
);
919 set_state(inc_dec_id
, sm
->name
, sm
->sym
, &incremented
);
922 static void handle_for_loops(struct expression
*expr
, char *state_name
, struct smatch_state
*false_state
)
925 char *iter_name
, *cap_name
;
926 struct symbol
*iter_sym
, *cap_sym
;
927 struct compare_data
*data
;
929 if (expr
->op
!= '<' && expr
->op
!= SPECIAL_UNSIGNED_LT
)
932 if (!__cur_stmt
|| !__prev_stmt
)
934 if (__cur_stmt
->type
!= STMT_ITERATOR
)
936 if (__cur_stmt
->iterator_pre_condition
!= expr
)
939 /* literals are handled in smatch_extra.c */
940 if (get_value(expr
->right
, &sval
))
943 /* First time checking the condition */
944 if (__prev_stmt
== __cur_stmt
->iterator_pre_statement
) {
945 if (!get_implied_value(expr
->left
, &sval
) ||
949 iter_name
= expr_to_var_sym(expr
->left
, &iter_sym
);
950 cap_name
= expr_to_var_sym(expr
->right
, &cap_sym
);
951 if (!iter_name
|| !cap_name
|| !iter_sym
|| !cap_sym
) {
952 free_string(iter_name
);
953 free_string(cap_name
);
957 set_state(inc_dec_id
, iter_name
, iter_sym
, &start
);
958 store_link(inc_dec_link_id
, cap_name
, cap_sym
, iter_name
, iter_sym
);
960 free_string(iter_name
);
961 free_string(cap_name
);
965 /* Second time checking the condtion */
966 if (__prev_stmt
!= __cur_stmt
->iterator_post_statement
)
969 if (get_state_chunk(inc_dec_id
, expr
->left
) != &incremented
)
972 data
= false_state
->data
;
973 false_state
= alloc_compare_state(data
->var1
, data
->vsl1
, SPECIAL_EQUAL
, data
->var2
, data
->vsl2
);
975 set_true_false_states(compare_id
, state_name
, NULL
, NULL
, false_state
);
978 static void handle_comparison(struct expression
*left_expr
, int op
, struct expression
*right_expr
, char **_state_name
, struct smatch_state
**_false_state
)
982 struct symbol
*left_sym
, *right_sym
;
983 struct var_sym_list
*left_vsl
, *right_vsl
;
986 struct smatch_state
*true_state
, *false_state
;
987 static char state_name
[256];
988 struct stree
*pre_stree
;
991 false_op
= negate_comparison(op
);
993 if (op
== SPECIAL_UNSIGNED_LT
&&
994 get_implied_value(left_expr
, &sval
) &&
996 false_op
= SPECIAL_EQUAL
;
998 if (op
== SPECIAL_UNSIGNED_GT
&&
999 get_implied_value(right_expr
, &sval
) &&
1001 false_op
= SPECIAL_EQUAL
;
1003 left
= chunk_to_var_sym(left_expr
, &left_sym
);
1006 left_vsl
= expr_to_vsl(left_expr
);
1007 right
= chunk_to_var_sym(right_expr
, &right_sym
);
1010 right_vsl
= expr_to_vsl(right_expr
);
1012 if (strcmp(left
, right
) > 0) {
1013 struct symbol
*tmp_sym
= left_sym
;
1014 char *tmp_name
= left
;
1015 struct var_sym_list
*tmp_vsl
= left_vsl
;
1016 struct expression
*tmp_expr
= left_expr
;
1019 left_sym
= right_sym
;
1020 left_vsl
= right_vsl
;
1021 left_expr
= right_expr
;
1023 right_sym
= tmp_sym
;
1024 right_vsl
= tmp_vsl
;
1025 right_expr
= tmp_expr
;
1026 op
= flip_comparison(op
);
1027 false_op
= flip_comparison(false_op
);
1030 orig_comparison
= get_comparison_strings(left
, right
);
1031 op
= filter_comparison(orig_comparison
, op
);
1032 false_op
= filter_comparison(orig_comparison
, false_op
);
1034 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left
, right
);
1035 true_state
= alloc_compare_state(left
, left_vsl
, op
, right
, right_vsl
);
1036 false_state
= alloc_compare_state(left
, left_vsl
, false_op
, right
, right_vsl
);
1038 pre_stree
= clone_stree(__get_cur_stree());
1039 update_tf_data(pre_stree
, left
, left_vsl
, right
, right_vsl
, op
, false_op
);
1040 free_stree(&pre_stree
);
1042 set_true_false_states(compare_id
, state_name
, NULL
, true_state
, false_state
);
1043 __compare_param_limit_hook(left_expr
, right_expr
, state_name
, true_state
, false_state
);
1044 save_link(left_expr
, state_name
);
1045 save_link(right_expr
, state_name
);
1048 *_false_state
= false_state
;
1050 *_state_name
= state_name
;
1057 void __comparison_match_condition(struct expression
*expr
)
1059 struct expression
*left
, *right
, *new_left
, *new_right
;
1060 struct smatch_state
*false_state
= NULL
;
1061 char *state_name
= NULL
;
1063 if (expr
->type
!= EXPR_COMPARE
)
1066 handle_comparison(expr
->left
, expr
->op
, expr
->right
, &state_name
, &false_state
);
1067 if (false_state
&& state_name
)
1068 handle_for_loops(expr
, state_name
, false_state
);
1071 left
= strip_parens(expr
->left
);
1072 right
= strip_parens(expr
->right
);
1074 if (left
->type
== EXPR_BINOP
&& left
->op
== '+') {
1075 new_left
= left
->left
;
1076 new_right
= binop_expression(right
, '-', left
->right
);
1077 handle_comparison(new_left
, expr
->op
, new_right
, NULL
, NULL
);
1079 new_left
= left
->right
;
1080 new_right
= binop_expression(right
, '-', left
->left
);
1081 handle_comparison(new_left
, expr
->op
, new_right
, NULL
, NULL
);
1085 static void add_comparison_var_sym(const char *left_name
,
1086 struct var_sym_list
*left_vsl
,
1088 const char *right_name
, struct var_sym_list
*right_vsl
)
1090 struct smatch_state
*state
;
1092 char state_name
[256];
1094 if (strcmp(left_name
, right_name
) > 0) {
1095 const char *tmp_name
= left_name
;
1096 struct var_sym_list
*tmp_vsl
= left_vsl
;
1098 left_name
= right_name
;
1099 left_vsl
= right_vsl
;
1100 right_name
= tmp_name
;
1101 right_vsl
= tmp_vsl
;
1102 comparison
= flip_comparison(comparison
);
1104 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left_name
, right_name
);
1105 state
= alloc_compare_state(left_name
, left_vsl
, comparison
, right_name
, right_vsl
);
1107 set_state(compare_id
, state_name
, NULL
, state
);
1109 FOR_EACH_PTR(left_vsl
, vs
) {
1110 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
1111 } END_FOR_EACH_PTR(vs
);
1112 FOR_EACH_PTR(right_vsl
, vs
) {
1113 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
1114 } END_FOR_EACH_PTR(vs
);
1117 static void add_comparison(struct expression
*left
, int comparison
, struct expression
*right
)
1119 char *left_name
= NULL
;
1120 char *right_name
= NULL
;
1121 struct symbol
*left_sym
, *right_sym
;
1122 struct var_sym_list
*left_vsl
, *right_vsl
;
1123 struct smatch_state
*state
;
1124 char state_name
[256];
1126 left_name
= chunk_to_var_sym(left
, &left_sym
);
1129 left_vsl
= expr_to_vsl(left
);
1130 right_name
= chunk_to_var_sym(right
, &right_sym
);
1133 right_vsl
= expr_to_vsl(right
);
1135 if (strcmp(left_name
, right_name
) > 0) {
1136 struct symbol
*tmp_sym
= left_sym
;
1137 char *tmp_name
= left_name
;
1138 struct var_sym_list
*tmp_vsl
= left_vsl
;
1140 left_name
= right_name
;
1141 left_sym
= right_sym
;
1142 left_vsl
= right_vsl
;
1143 right_name
= tmp_name
;
1144 right_sym
= tmp_sym
;
1145 right_vsl
= tmp_vsl
;
1146 comparison
= flip_comparison(comparison
);
1148 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left_name
, right_name
);
1149 state
= alloc_compare_state(left_name
, left_vsl
, comparison
, right_name
, right_vsl
);
1151 set_state(compare_id
, state_name
, NULL
, state
);
1152 save_link(left
, state_name
);
1153 save_link(right
, state_name
);
1156 free_string(left_name
);
1157 free_string(right_name
);
1160 static void match_assign_add(struct expression
*expr
)
1162 struct expression
*right
;
1163 struct expression
*r_left
, *r_right
;
1164 sval_t left_tmp
, right_tmp
;
1166 right
= strip_expr(expr
->right
);
1167 r_left
= strip_expr(right
->left
);
1168 r_right
= strip_expr(right
->right
);
1170 get_absolute_min(r_left
, &left_tmp
);
1171 get_absolute_min(r_right
, &right_tmp
);
1173 if (left_tmp
.value
> 0)
1174 add_comparison(expr
->left
, '>', r_right
);
1175 else if (left_tmp
.value
== 0)
1176 add_comparison(expr
->left
, SPECIAL_GTE
, r_right
);
1178 if (right_tmp
.value
> 0)
1179 add_comparison(expr
->left
, '>', r_left
);
1180 else if (right_tmp
.value
== 0)
1181 add_comparison(expr
->left
, SPECIAL_GTE
, r_left
);
1184 static void match_assign_sub(struct expression
*expr
)
1186 struct expression
*right
;
1187 struct expression
*r_left
, *r_right
;
1191 right
= strip_expr(expr
->right
);
1192 r_left
= strip_expr(right
->left
);
1193 r_right
= strip_expr(right
->right
);
1195 if (get_absolute_min(r_right
, &min
) && sval_is_negative(min
))
1198 comparison
= get_comparison(r_left
, r_right
);
1200 switch (comparison
) {
1203 if (implied_not_equal(r_right
, 0))
1204 add_comparison(expr
->left
, '>', r_left
);
1206 add_comparison(expr
->left
, SPECIAL_GTE
, r_left
);
1211 static void match_assign_divide(struct expression
*expr
)
1213 struct expression
*right
;
1214 struct expression
*r_left
, *r_right
;
1217 right
= strip_expr(expr
->right
);
1218 r_left
= strip_expr(right
->left
);
1219 r_right
= strip_expr(right
->right
);
1220 if (!get_implied_min(r_right
, &min
) || min
.value
<= 1)
1223 add_comparison(expr
->left
, '<', r_left
);
1226 static void match_binop_assign(struct expression
*expr
)
1228 struct expression
*right
;
1230 right
= strip_expr(expr
->right
);
1231 if (right
->op
== '+')
1232 match_assign_add(expr
);
1233 if (right
->op
== '-')
1234 match_assign_sub(expr
);
1235 if (right
->op
== '/')
1236 match_assign_divide(expr
);
1239 static void copy_comparisons(struct expression
*left
, struct expression
*right
)
1241 struct string_list
*links
;
1242 struct smatch_state
*state
;
1243 struct compare_data
*data
;
1244 struct symbol
*left_sym
, *right_sym
;
1245 char *left_var
= NULL
;
1246 char *right_var
= NULL
;
1247 struct var_sym_list
*left_vsl
;
1249 struct var_sym_list
*vsl
;
1253 left_var
= chunk_to_var_sym(left
, &left_sym
);
1256 left_vsl
= expr_to_vsl(left
);
1257 right_var
= chunk_to_var_sym(right
, &right_sym
);
1261 state
= get_state(link_id
, right_var
, right_sym
);
1264 links
= state
->data
;
1266 FOR_EACH_PTR(links
, tmp
) {
1267 state
= get_state(compare_id
, tmp
, NULL
);
1268 if (!state
|| !state
->data
)
1271 comparison
= data
->comparison
;
1274 if (strcmp(var
, right_var
) == 0) {
1277 comparison
= flip_comparison(comparison
);
1279 add_comparison_var_sym(left_var
, left_vsl
, comparison
, var
, vsl
);
1280 } END_FOR_EACH_PTR(tmp
);
1283 free_string(right_var
);
1286 static void match_assign(struct expression
*expr
)
1288 struct expression
*right
;
1290 if (expr
->op
!= '=')
1292 if (__in_fake_assign
|| outside_of_function())
1295 if (is_struct(expr
->left
))
1298 copy_comparisons(expr
->left
, expr
->right
);
1299 add_comparison(expr
->left
, SPECIAL_EQUAL
, expr
->right
);
1301 right
= strip_expr(expr
->right
);
1302 if (right
->type
== EXPR_BINOP
)
1303 match_binop_assign(expr
);
1306 int get_comparison_strings(const char *one
, const char *two
)
1309 struct smatch_state
*state
;
1313 if (strcmp(one
, two
) == 0)
1314 return SPECIAL_EQUAL
;
1316 if (strcmp(one
, two
) > 0) {
1317 const char *tmp
= one
;
1324 snprintf(buf
, sizeof(buf
), "%s vs %s", one
, two
);
1325 state
= get_state(compare_id
, buf
, NULL
);
1327 ret
= state_to_comparison(state
);
1330 ret
= flip_comparison(ret
);
1335 int get_comparison(struct expression
*a
, struct expression
*b
)
1341 one
= chunk_to_var(a
);
1344 two
= chunk_to_var(b
);
1348 ret
= get_comparison_strings(one
, two
);
1355 int possible_comparison(struct expression
*a
, int comparison
, struct expression
*b
)
1361 struct sm_state
*sm
;
1364 one
= chunk_to_var(a
);
1367 two
= chunk_to_var(b
);
1372 if (strcmp(one
, two
) == 0 && comparison
== SPECIAL_EQUAL
) {
1377 if (strcmp(one
, two
) > 0) {
1382 comparison
= flip_comparison(comparison
);
1385 snprintf(buf
, sizeof(buf
), "%s vs %s", one
, two
);
1386 sm
= get_sm_state(compare_id
, buf
, NULL
);
1390 FOR_EACH_PTR(sm
->possible
, sm
) {
1391 if (!sm
->state
->data
)
1393 saved
= ((struct compare_data
*)sm
->state
->data
)->comparison
;
1394 if (saved
== comparison
)
1396 if (comparison
== SPECIAL_EQUAL
&&
1397 (saved
== SPECIAL_LTE
||
1398 saved
== SPECIAL_GTE
||
1399 saved
== SPECIAL_UNSIGNED_LTE
||
1400 saved
== SPECIAL_UNSIGNED_GTE
))
1404 } END_FOR_EACH_PTR(sm
);
1413 struct state_list
*get_all_comparisons(struct expression
*expr
)
1415 struct smatch_state
*state
;
1416 struct string_list
*links
;
1417 struct state_list
*ret
= NULL
;
1418 struct sm_state
*sm
;
1421 state
= get_state_chunk(link_id
, expr
);
1424 links
= state
->data
;
1426 FOR_EACH_PTR(links
, tmp
) {
1427 sm
= get_sm_state(compare_id
, tmp
, NULL
);
1430 // FIXME have to compare name with vsl
1431 add_ptr_list(&ret
, sm
);
1432 } END_FOR_EACH_PTR(tmp
);
1437 struct state_list
*get_all_possible_equal_comparisons(struct expression
*expr
)
1439 struct smatch_state
*state
;
1440 struct string_list
*links
;
1441 struct state_list
*ret
= NULL
;
1442 struct sm_state
*sm
;
1445 state
= get_state_chunk(link_id
, expr
);
1448 links
= state
->data
;
1450 FOR_EACH_PTR(links
, tmp
) {
1451 sm
= get_sm_state(compare_id
, tmp
, NULL
);
1454 if (!strchr(sm
->state
->name
, '='))
1456 if (strcmp(sm
->state
->name
, "!=") == 0)
1458 add_ptr_list(&ret
, sm
);
1459 } END_FOR_EACH_PTR(tmp
);
1464 static void update_links_from_call(struct expression
*left
,
1466 struct expression
*right
)
1468 struct string_list
*links
;
1469 struct smatch_state
*state
;
1470 struct compare_data
*data
;
1471 struct symbol
*left_sym
, *right_sym
;
1472 char *left_var
= NULL
;
1473 char *right_var
= NULL
;
1474 struct var_sym_list
*left_vsl
;
1476 struct var_sym_list
*vsl
;
1480 left_var
= chunk_to_var_sym(left
, &left_sym
);
1483 left_vsl
= expr_to_vsl(left
);
1484 right_var
= chunk_to_var_sym(right
, &right_sym
);
1488 state
= get_state(link_id
, right_var
, right_sym
);
1491 links
= state
->data
;
1493 FOR_EACH_PTR(links
, tmp
) {
1494 state
= get_state(compare_id
, tmp
, NULL
);
1495 if (!state
|| !state
->data
)
1498 comparison
= data
->comparison
;
1501 if (strcmp(var
, right_var
) == 0) {
1504 comparison
= flip_comparison(comparison
);
1506 comparison
= combine_comparisons(left_compare
, comparison
);
1509 add_comparison_var_sym(left_var
, left_vsl
, comparison
, var
, vsl
);
1510 } END_FOR_EACH_PTR(tmp
);
1513 free_string(right_var
);
1516 void __add_return_comparison(struct expression
*call
, const char *range
)
1518 struct expression
*arg
;
1522 if (!str_to_comparison_arg(range
, call
, &comparison
, &arg
))
1524 snprintf(buf
, sizeof(buf
), "%s", show_special(comparison
));
1526 sm_msg("setting %s to %s", expr_to_str(call
), buf
);
1527 update_links_from_call(call
, comparison
, arg
);
1528 add_comparison(call
, comparison
, arg
);
1531 void __add_comparison_info(struct expression
*expr
, struct expression
*call
, const char *range
)
1533 copy_comparisons(expr
, call
);
1536 static char *range_comparison_to_param_helper(struct expression
*expr
, char starts_with
, int ignore
)
1538 struct symbol
*param
;
1541 char *ret_str
= NULL
;
1545 var
= chunk_to_var(expr
);
1550 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
1556 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
1557 compare
= get_comparison_strings(var
, buf
);
1560 if (show_special(compare
)[0] != starts_with
)
1562 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(compare
), i
);
1563 ret_str
= alloc_sname(buf
);
1565 } END_FOR_EACH_PTR(param
);
1572 char *name_sym_to_param_comparison(const char *name
, struct symbol
*sym
)
1574 struct symbol
*param
;
1580 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
1584 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
1585 compare
= get_comparison_strings(name
, buf
);
1588 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(compare
), i
);
1589 return alloc_sname(buf
);
1590 } END_FOR_EACH_PTR(param
);
1595 char *expr_equal_to_param(struct expression
*expr
, int ignore
)
1597 return range_comparison_to_param_helper(expr
, '=', ignore
);
1600 char *expr_lte_to_param(struct expression
*expr
, int ignore
)
1602 return range_comparison_to_param_helper(expr
, '<', ignore
);
1605 char *expr_param_comparison(struct expression
*expr
, int ignore
)
1607 struct symbol
*param
;
1610 char *ret_str
= NULL
;
1614 var
= chunk_to_var(expr
);
1619 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
1625 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
1626 compare
= get_comparison_strings(var
, buf
);
1629 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(compare
), i
);
1630 ret_str
= alloc_sname(buf
);
1632 } END_FOR_EACH_PTR(param
);
1639 char *get_printed_param_name(struct expression
*call
, const char *param_name
, struct symbol
*param_sym
)
1641 struct expression
*arg
;
1644 static char buf
[256];
1649 FOR_EACH_PTR(call
->args
, arg
) {
1652 name
= expr_to_var_sym(arg
, &sym
);
1655 if (sym
!= param_sym
)
1659 if (strncmp(name
, param_name
, len
) != 0)
1661 if (param_name
[len
] == '\0') {
1662 snprintf(buf
, sizeof(buf
), "$%d", i
);
1665 if (param_name
[len
] != '-')
1667 snprintf(buf
, sizeof(buf
), "$%d%s", i
, param_name
+ len
);
1669 } END_FOR_EACH_PTR(arg
);
1674 static void match_call_info(struct expression
*expr
)
1676 struct expression
*arg
;
1677 struct smatch_state
*state
;
1678 struct sm_state
*sm
;
1679 struct compare_data
*data
;
1681 struct string_list
*links
;
1683 const char *right_name
;
1689 FOR_EACH_PTR(expr
->args
, arg
) {
1692 state
= get_state_chunk(link_id
, arg
);
1696 links
= state
->data
;
1697 FOR_EACH_PTR(links
, link
) {
1698 struct var_sym_list
*right_vsl
;
1699 struct var_sym
*right_vs
;
1702 if (strstr(link
, " orig"))
1704 sm
= get_sm_state(compare_id
, link
, NULL
);
1707 data
= sm
->state
->data
;
1708 if (!data
|| !data
->comparison
)
1710 arg_name
= expr_to_var(arg
);
1715 if (strcmp(data
->var1
, arg_name
) == 0) {
1716 comparison
= data
->comparison
;
1717 right_name
= data
->var2
;
1718 right_vsl
= data
->vsl2
;
1719 } else if (strcmp(data
->var2
, arg_name
) == 0) {
1720 comparison
= flip_comparison(data
->comparison
);
1721 right_name
= data
->var1
;
1722 right_vsl
= data
->vsl1
;
1724 if (!right_vsl
|| ptr_list_size((struct ptr_list
*)right_vsl
) != 1)
1727 right_vs
= first_ptr_list((struct ptr_list
*)right_vsl
);
1728 if (strcmp(right_vs
->var
, right_name
) != 0)
1730 right_name
= get_printed_param_name(expr
, right_vs
->var
, right_vs
->sym
);
1733 snprintf(info_buf
, sizeof(info_buf
), "%s %s", show_special(comparison
), right_name
);
1734 sql_insert_caller_info(expr
, PARAM_COMPARE
, i
, "$", info_buf
);
1737 free_string(arg_name
);
1738 } END_FOR_EACH_PTR(link
);
1739 } END_FOR_EACH_PTR(arg
);
1742 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*link_sm
)
1744 struct sm_state
*compare_sm
;
1745 struct string_list
*links
;
1747 struct compare_data
*data
;
1748 struct var_sym
*left
, *right
;
1749 static char info_buf
[256];
1750 const char *right_name
;
1752 if (strstr(printed_name
, " orig"))
1755 links
= link_sm
->state
->data
;
1756 FOR_EACH_PTR(links
, link
) {
1757 compare_sm
= get_sm_state(compare_id
, link
, NULL
);
1760 data
= compare_sm
->state
->data
;
1761 if (!data
|| !data
->comparison
)
1764 if (ptr_list_size((struct ptr_list
*)data
->vsl1
) != 1 ||
1765 ptr_list_size((struct ptr_list
*)data
->vsl2
) != 1)
1767 left
= first_ptr_list((struct ptr_list
*)data
->vsl1
);
1768 right
= first_ptr_list((struct ptr_list
*)data
->vsl2
);
1769 if (left
->sym
== right
->sym
&&
1770 strcmp(left
->var
, right
->var
) == 0)
1773 * Both parameters link to this comparison so only
1774 * record the first one.
1776 if (left
->sym
!= link_sm
->sym
||
1777 strcmp(left
->var
, link_sm
->name
) != 0)
1780 right_name
= get_printed_param_name(call
, right
->var
, right
->sym
);
1783 snprintf(info_buf
, sizeof(info_buf
), "%s %s", show_special(data
->comparison
), right_name
);
1784 sql_insert_caller_info(call
, PARAM_COMPARE
, param
, printed_name
, info_buf
);
1785 } END_FOR_EACH_PTR(link
);
1788 static void print_return_value_comparison(int return_id
, char *return_ranges
, struct expression
*expr
)
1791 const char *tmp_name
;
1797 * TODO: This only prints == comparisons. That's probably the most
1798 * useful comparison because == max has lots of implications. But it
1799 * would be good to capture the rest as well.
1801 * This information is already in the DB but it's in the parameter math
1802 * bits and it's awkward to use it. This is is the simpler, possibly
1803 * cleaner way, but not necessarily the best, I don't know.
1808 name
= expr_to_var_sym(expr
, &sym
);
1812 param
= get_param_num_from_sym(sym
);
1815 if (param_was_set_var_sym(name
, sym
))
1818 tmp_name
= get_param_name_var_sym(name
, sym
);
1822 snprintf(info_buf
, sizeof(info_buf
), "== $%d%s", param
, tmp_name
+ 1);
1823 sql_insert_return_states(return_id
, return_ranges
,
1824 PARAM_COMPARE
, -1, "$", info_buf
);
1829 static void print_return_comparison(int return_id
, char *return_ranges
, struct expression
*expr
)
1831 struct sm_state
*tmp
;
1832 struct string_list
*links
;
1834 struct sm_state
*sm
;
1835 struct compare_data
*data
;
1836 struct var_sym
*left
, *right
;
1837 int left_param
, right_param
;
1839 char right_buf
[256];
1841 const char *tmp_name
;
1843 print_return_value_comparison(return_id
, return_ranges
, expr
);
1845 FOR_EACH_MY_SM(link_id
, __get_cur_stree(), tmp
) {
1846 if (get_param_num_from_sym(tmp
->sym
) < 0)
1848 links
= tmp
->state
->data
;
1849 FOR_EACH_PTR(links
, link
) {
1850 sm
= get_sm_state(compare_id
, link
, NULL
);
1853 data
= sm
->state
->data
;
1854 if (!data
|| !data
->comparison
)
1856 if (ptr_list_size((struct ptr_list
*)data
->vsl1
) != 1 ||
1857 ptr_list_size((struct ptr_list
*)data
->vsl2
) != 1)
1859 left
= first_ptr_list((struct ptr_list
*)data
->vsl1
);
1860 right
= first_ptr_list((struct ptr_list
*)data
->vsl2
);
1861 if (left
->sym
== right
->sym
&&
1862 strcmp(left
->var
, right
->var
) == 0)
1865 * Both parameters link to this comparison so only
1866 * record the first one.
1868 if (left
->sym
!= tmp
->sym
||
1869 strcmp(left
->var
, tmp
->name
) != 0)
1872 if (strstr(right
->var
, " orig"))
1875 left_param
= get_param_num_from_sym(left
->sym
);
1876 right_param
= get_param_num_from_sym(right
->sym
);
1877 if (left_param
< 0 || right_param
< 0)
1880 tmp_name
= get_param_name_var_sym(left
->var
, left
->sym
);
1883 snprintf(left_buf
, sizeof(left_buf
), "%s", tmp_name
);
1885 tmp_name
= get_param_name_var_sym(right
->var
, right
->sym
);
1886 if (!tmp_name
|| tmp_name
[0] != '$')
1888 snprintf(right_buf
, sizeof(right_buf
), "$%d%s", right_param
, tmp_name
+ 1);
1891 * FIXME: this should reject $ type variables (as
1892 * opposed to $->foo type). Those should come from
1893 * smatch_param_compare_limit.c.
1896 snprintf(info_buf
, sizeof(info_buf
), "%s %s", show_special(data
->comparison
), right_buf
);
1897 sql_insert_return_states(return_id
, return_ranges
,
1898 PARAM_COMPARE
, left_param
, left_buf
, info_buf
);
1899 } END_FOR_EACH_PTR(link
);
1901 } END_FOR_EACH_SM(tmp
);
1904 static int parse_comparison(char **value
, int *op
)
1912 if (**value
== '=') {
1920 *op
= SPECIAL_EQUAL
;
1925 *op
= SPECIAL_NOTEQUAL
;
1929 if (**value
== '=') {
1938 if (**value
!= ' ') {
1939 sm_msg("internal error parsing comparison. %s", *value
);
1947 static int split_op_param_key(char *value
, int *op
, int *param
, char **key
)
1949 static char buf
[256];
1952 if (!parse_comparison(&value
, op
))
1955 snprintf(buf
, sizeof(buf
), value
);
1962 if (*param
< 0 || *param
> 99)
1974 static void db_return_comparison(struct expression
*expr
, int left_param
, char *key
, char *value
)
1976 struct expression
*left_arg
, *right_arg
;
1977 char *left_name
= NULL
;
1978 struct symbol
*left_sym
;
1979 char *right_name
= NULL
;
1980 struct symbol
*right_sym
;
1984 struct var_sym_list
*left_vsl
= NULL
, *right_vsl
= NULL
;
1986 if (left_param
== -1) {
1987 if (expr
->type
!= EXPR_ASSIGNMENT
)
1989 left_arg
= strip_expr(expr
->left
);
1991 while (expr
->type
== EXPR_ASSIGNMENT
)
1992 expr
= strip_expr(expr
->right
);
1993 if (expr
->type
!= EXPR_CALL
)
1996 while (expr
->type
== EXPR_ASSIGNMENT
)
1997 expr
= strip_expr(expr
->right
);
1998 if (expr
->type
!= EXPR_CALL
)
2001 left_arg
= get_argument_from_call_expr(expr
->args
, left_param
);
2006 if (!split_op_param_key(value
, &op
, &right_param
, &right_key
))
2009 right_arg
= get_argument_from_call_expr(expr
->args
, right_param
);
2013 left_name
= get_variable_from_key(left_arg
, key
, &left_sym
);
2014 if (!left_name
|| !left_sym
)
2017 right_name
= get_variable_from_key(right_arg
, right_key
, &right_sym
);
2018 if (!right_name
|| !right_sym
)
2021 add_var_sym(&left_vsl
, left_name
, left_sym
);
2022 add_var_sym(&right_vsl
, right_name
, right_sym
);
2024 add_comparison_var_sym(left_name
, left_vsl
, op
, right_name
, right_vsl
);
2027 free_string(left_name
);
2028 free_string(right_name
);
2031 int param_compare_limit_is_impossible(struct expression
*expr
, int left_param
, char *left_key
, char *value
)
2033 struct smatch_state
*state
;
2034 char *left_name
= NULL
;
2035 char *right_name
= NULL
;
2036 struct symbol
*left_sym
, *right_sym
;
2037 struct expression
*left_arg
, *right_arg
;
2044 while (expr
->type
== EXPR_ASSIGNMENT
)
2045 expr
= strip_expr(expr
->right
);
2046 if (expr
->type
!= EXPR_CALL
)
2049 if (!split_op_param_key(value
, &op
, &right_param
, &right_key
))
2052 left_arg
= get_argument_from_call_expr(expr
->args
, left_param
);
2056 right_arg
= get_argument_from_call_expr(expr
->args
, right_param
);
2060 left_name
= get_variable_from_key(left_arg
, left_key
, &left_sym
);
2061 right_name
= get_variable_from_key(right_arg
, right_key
, &right_sym
);
2062 if (!left_name
|| !right_name
)
2065 snprintf(buf
, sizeof(buf
), "%s vs %s", left_name
, right_name
);
2066 state
= get_state(compare_id
, buf
, NULL
);
2069 if (!state_to_comparison(state
))
2072 if (!filter_comparison(state_to_comparison(state
), op
))
2075 free_string(left_name
);
2076 free_string(right_name
);
2080 static void free_data(struct symbol
*sym
)
2084 clear_compare_data_alloc();
2087 void register_comparison(int id
)
2090 add_hook(&save_start_states
, AFTER_DEF_HOOK
);
2091 add_unmatched_state_hook(compare_id
, unmatched_comparison
);
2092 add_merge_hook(compare_id
, &merge_compare_states
);
2093 add_hook(&free_data
, AFTER_FUNC_HOOK
);
2094 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
2095 add_split_return_callback(&print_return_comparison
);
2097 select_return_states_hook(PARAM_COMPARE
, &db_return_comparison
);
2100 void register_comparison_late(int id
)
2102 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
2105 void register_comparison_links(int id
)
2108 add_merge_hook(link_id
, &merge_links
);
2109 add_modification_hook(link_id
, &match_modify
);
2111 add_member_info_callback(link_id
, struct_member_callback
);
2114 void register_comparison_inc_dec(int id
)
2117 add_modification_hook_late(inc_dec_id
, &iter_modify
);
2120 void register_comparison_inc_dec_links(int id
)
2122 inc_dec_link_id
= id
;
2123 set_up_link_functions(inc_dec_id
, inc_dec_link_id
);
2126 static void filter_by_sm(struct sm_state
*sm
, int op
,
2127 struct state_list
**true_stack
,
2128 struct state_list
**false_stack
)
2130 struct compare_data
*data
;
2136 data
= sm
->state
->data
;
2139 filter_by_sm(sm
->left
, op
, true_stack
, false_stack
);
2140 filter_by_sm(sm
->right
, op
, true_stack
, false_stack
);
2145 if (data
->comparison
&&
2146 data
->comparison
== filter_comparison(data
->comparison
, op
))
2149 if (data
->comparison
&&
2150 data
->comparison
== filter_comparison(data
->comparison
, negate_comparison(op
)))
2154 add_ptr_list(true_stack
, sm
);
2156 add_ptr_list(false_stack
, sm
);
2159 filter_by_sm(sm
->left
, op
, true_stack
, false_stack
);
2160 filter_by_sm(sm
->right
, op
, true_stack
, false_stack
);
2164 struct sm_state
*comparison_implication_hook(struct expression
*expr
,
2165 struct state_list
**true_stack
,
2166 struct state_list
**false_stack
)
2168 struct sm_state
*sm
;
2171 static char buf
[256];
2173 if (expr
->type
!= EXPR_COMPARE
)
2178 left
= expr_to_var(expr
->left
);
2179 right
= expr_to_var(expr
->right
);
2180 if (!left
|| !right
) {
2186 if (strcmp(left
, right
) > 0) {
2191 op
= flip_comparison(op
);
2194 snprintf(buf
, sizeof(buf
), "%s vs %s", left
, right
);
2195 sm
= get_sm_state(compare_id
, buf
, NULL
);
2201 filter_by_sm(sm
, op
, true_stack
, false_stack
);
2202 if (!*true_stack
&& !*false_stack
)