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
));
1525 update_links_from_call(call
, comparison
, arg
);
1526 add_comparison(call
, comparison
, arg
);
1529 void __add_comparison_info(struct expression
*expr
, struct expression
*call
, const char *range
)
1531 copy_comparisons(expr
, call
);
1534 static char *range_comparison_to_param_helper(struct expression
*expr
, char starts_with
, int ignore
)
1536 struct symbol
*param
;
1539 char *ret_str
= NULL
;
1543 var
= chunk_to_var(expr
);
1548 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
1554 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
1555 compare
= get_comparison_strings(var
, buf
);
1558 if (show_special(compare
)[0] != starts_with
)
1560 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(compare
), i
);
1561 ret_str
= alloc_sname(buf
);
1563 } END_FOR_EACH_PTR(param
);
1570 char *name_sym_to_param_comparison(const char *name
, struct symbol
*sym
)
1572 struct symbol
*param
;
1578 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
1582 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
1583 compare
= get_comparison_strings(name
, buf
);
1586 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(compare
), i
);
1587 return alloc_sname(buf
);
1588 } END_FOR_EACH_PTR(param
);
1593 char *expr_equal_to_param(struct expression
*expr
, int ignore
)
1595 return range_comparison_to_param_helper(expr
, '=', ignore
);
1598 char *expr_lte_to_param(struct expression
*expr
, int ignore
)
1600 return range_comparison_to_param_helper(expr
, '<', ignore
);
1603 char *expr_param_comparison(struct expression
*expr
, int ignore
)
1605 struct symbol
*param
;
1608 char *ret_str
= NULL
;
1612 var
= chunk_to_var(expr
);
1617 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
1623 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
1624 compare
= get_comparison_strings(var
, buf
);
1627 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(compare
), i
);
1628 ret_str
= alloc_sname(buf
);
1630 } END_FOR_EACH_PTR(param
);
1637 char *get_printed_param_name(struct expression
*call
, const char *param_name
, struct symbol
*param_sym
)
1639 struct expression
*arg
;
1642 static char buf
[256];
1647 FOR_EACH_PTR(call
->args
, arg
) {
1650 name
= expr_to_var_sym(arg
, &sym
);
1653 if (sym
!= param_sym
)
1657 if (strncmp(name
, param_name
, len
) != 0)
1659 if (param_name
[len
] == '\0') {
1660 snprintf(buf
, sizeof(buf
), "$%d", i
);
1663 if (param_name
[len
] != '-')
1665 snprintf(buf
, sizeof(buf
), "$%d%s", i
, param_name
+ len
);
1667 } END_FOR_EACH_PTR(arg
);
1672 static void match_call_info(struct expression
*expr
)
1674 struct expression
*arg
;
1675 struct smatch_state
*state
;
1676 struct sm_state
*sm
;
1677 struct compare_data
*data
;
1679 struct string_list
*links
;
1681 const char *right_name
;
1687 FOR_EACH_PTR(expr
->args
, arg
) {
1690 state
= get_state_chunk(link_id
, arg
);
1694 links
= state
->data
;
1695 FOR_EACH_PTR(links
, link
) {
1696 struct var_sym_list
*right_vsl
;
1697 struct var_sym
*right_vs
;
1700 if (strstr(link
, " orig"))
1702 sm
= get_sm_state(compare_id
, link
, NULL
);
1705 data
= sm
->state
->data
;
1706 if (!data
|| !data
->comparison
)
1708 arg_name
= expr_to_var(arg
);
1713 if (strcmp(data
->var1
, arg_name
) == 0) {
1714 comparison
= data
->comparison
;
1715 right_name
= data
->var2
;
1716 right_vsl
= data
->vsl2
;
1717 } else if (strcmp(data
->var2
, arg_name
) == 0) {
1718 comparison
= flip_comparison(data
->comparison
);
1719 right_name
= data
->var1
;
1720 right_vsl
= data
->vsl1
;
1722 if (!right_vsl
|| ptr_list_size((struct ptr_list
*)right_vsl
) != 1)
1725 right_vs
= first_ptr_list((struct ptr_list
*)right_vsl
);
1726 if (strcmp(right_vs
->var
, right_name
) != 0)
1728 right_name
= get_printed_param_name(expr
, right_vs
->var
, right_vs
->sym
);
1731 snprintf(info_buf
, sizeof(info_buf
), "%s %s", show_special(comparison
), right_name
);
1732 sql_insert_caller_info(expr
, PARAM_COMPARE
, i
, "$", info_buf
);
1735 free_string(arg_name
);
1736 } END_FOR_EACH_PTR(link
);
1737 } END_FOR_EACH_PTR(arg
);
1740 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*link_sm
)
1742 struct sm_state
*compare_sm
;
1743 struct string_list
*links
;
1745 struct compare_data
*data
;
1746 struct var_sym
*left
, *right
;
1747 static char info_buf
[256];
1748 const char *right_name
;
1750 if (strstr(printed_name
, " orig"))
1753 links
= link_sm
->state
->data
;
1754 FOR_EACH_PTR(links
, link
) {
1755 compare_sm
= get_sm_state(compare_id
, link
, NULL
);
1758 data
= compare_sm
->state
->data
;
1759 if (!data
|| !data
->comparison
)
1762 if (ptr_list_size((struct ptr_list
*)data
->vsl1
) != 1 ||
1763 ptr_list_size((struct ptr_list
*)data
->vsl2
) != 1)
1765 left
= first_ptr_list((struct ptr_list
*)data
->vsl1
);
1766 right
= first_ptr_list((struct ptr_list
*)data
->vsl2
);
1767 if (left
->sym
== right
->sym
&&
1768 strcmp(left
->var
, right
->var
) == 0)
1771 * Both parameters link to this comparison so only
1772 * record the first one.
1774 if (left
->sym
!= link_sm
->sym
||
1775 strcmp(left
->var
, link_sm
->name
) != 0)
1778 right_name
= get_printed_param_name(call
, right
->var
, right
->sym
);
1781 snprintf(info_buf
, sizeof(info_buf
), "%s %s", show_special(data
->comparison
), right_name
);
1782 sql_insert_caller_info(call
, PARAM_COMPARE
, param
, printed_name
, info_buf
);
1783 } END_FOR_EACH_PTR(link
);
1786 static void print_return_value_comparison(int return_id
, char *return_ranges
, struct expression
*expr
)
1789 const char *tmp_name
;
1795 * TODO: This only prints == comparisons. That's probably the most
1796 * useful comparison because == max has lots of implications. But it
1797 * would be good to capture the rest as well.
1799 * This information is already in the DB but it's in the parameter math
1800 * bits and it's awkward to use it. This is is the simpler, possibly
1801 * cleaner way, but not necessarily the best, I don't know.
1806 name
= expr_to_var_sym(expr
, &sym
);
1810 param
= get_param_num_from_sym(sym
);
1813 if (param_was_set_var_sym(name
, sym
))
1816 tmp_name
= get_param_name_var_sym(name
, sym
);
1820 snprintf(info_buf
, sizeof(info_buf
), "== $%d%s", param
, tmp_name
+ 1);
1821 sql_insert_return_states(return_id
, return_ranges
,
1822 PARAM_COMPARE
, -1, "$", info_buf
);
1827 static void print_return_comparison(int return_id
, char *return_ranges
, struct expression
*expr
)
1829 struct sm_state
*tmp
;
1830 struct string_list
*links
;
1832 struct sm_state
*sm
;
1833 struct compare_data
*data
;
1834 struct var_sym
*left
, *right
;
1835 int left_param
, right_param
;
1837 char right_buf
[256];
1839 const char *tmp_name
;
1841 print_return_value_comparison(return_id
, return_ranges
, expr
);
1843 FOR_EACH_MY_SM(link_id
, __get_cur_stree(), tmp
) {
1844 if (get_param_num_from_sym(tmp
->sym
) < 0)
1846 links
= tmp
->state
->data
;
1847 FOR_EACH_PTR(links
, link
) {
1848 sm
= get_sm_state(compare_id
, link
, NULL
);
1851 data
= sm
->state
->data
;
1852 if (!data
|| !data
->comparison
)
1854 if (ptr_list_size((struct ptr_list
*)data
->vsl1
) != 1 ||
1855 ptr_list_size((struct ptr_list
*)data
->vsl2
) != 1)
1857 left
= first_ptr_list((struct ptr_list
*)data
->vsl1
);
1858 right
= first_ptr_list((struct ptr_list
*)data
->vsl2
);
1859 if (left
->sym
== right
->sym
&&
1860 strcmp(left
->var
, right
->var
) == 0)
1863 * Both parameters link to this comparison so only
1864 * record the first one.
1866 if (left
->sym
!= tmp
->sym
||
1867 strcmp(left
->var
, tmp
->name
) != 0)
1870 if (strstr(right
->var
, " orig"))
1873 left_param
= get_param_num_from_sym(left
->sym
);
1874 right_param
= get_param_num_from_sym(right
->sym
);
1875 if (left_param
< 0 || right_param
< 0)
1878 tmp_name
= get_param_name_var_sym(left
->var
, left
->sym
);
1881 snprintf(left_buf
, sizeof(left_buf
), "%s", tmp_name
);
1883 tmp_name
= get_param_name_var_sym(right
->var
, right
->sym
);
1884 if (!tmp_name
|| tmp_name
[0] != '$')
1886 snprintf(right_buf
, sizeof(right_buf
), "$%d%s", right_param
, tmp_name
+ 1);
1889 * FIXME: this should reject $ type variables (as
1890 * opposed to $->foo type). Those should come from
1891 * smatch_param_compare_limit.c.
1894 snprintf(info_buf
, sizeof(info_buf
), "%s %s", show_special(data
->comparison
), right_buf
);
1895 sql_insert_return_states(return_id
, return_ranges
,
1896 PARAM_COMPARE
, left_param
, left_buf
, info_buf
);
1897 } END_FOR_EACH_PTR(link
);
1899 } END_FOR_EACH_SM(tmp
);
1902 static int parse_comparison(char **value
, int *op
)
1910 if (**value
== '=') {
1918 *op
= SPECIAL_EQUAL
;
1923 *op
= SPECIAL_NOTEQUAL
;
1927 if (**value
== '=') {
1936 if (**value
!= ' ') {
1937 sm_msg("internal error parsing comparison. %s", *value
);
1945 static int split_op_param_key(char *value
, int *op
, int *param
, char **key
)
1947 static char buf
[256];
1950 if (!parse_comparison(&value
, op
))
1953 snprintf(buf
, sizeof(buf
), value
);
1960 if (*param
< 0 || *param
> 99)
1972 static void db_return_comparison(struct expression
*expr
, int left_param
, char *key
, char *value
)
1974 struct expression
*left_arg
, *right_arg
;
1975 char *left_name
= NULL
;
1976 struct symbol
*left_sym
;
1977 char *right_name
= NULL
;
1978 struct symbol
*right_sym
;
1982 struct var_sym_list
*left_vsl
= NULL
, *right_vsl
= NULL
;
1984 if (left_param
== -1) {
1985 if (expr
->type
!= EXPR_ASSIGNMENT
)
1987 left_arg
= strip_expr(expr
->left
);
1989 while (expr
->type
== EXPR_ASSIGNMENT
)
1990 expr
= strip_expr(expr
->right
);
1991 if (expr
->type
!= EXPR_CALL
)
1994 while (expr
->type
== EXPR_ASSIGNMENT
)
1995 expr
= strip_expr(expr
->right
);
1996 if (expr
->type
!= EXPR_CALL
)
1999 left_arg
= get_argument_from_call_expr(expr
->args
, left_param
);
2004 if (!split_op_param_key(value
, &op
, &right_param
, &right_key
))
2007 right_arg
= get_argument_from_call_expr(expr
->args
, right_param
);
2011 left_name
= get_variable_from_key(left_arg
, key
, &left_sym
);
2012 if (!left_name
|| !left_sym
)
2015 right_name
= get_variable_from_key(right_arg
, right_key
, &right_sym
);
2016 if (!right_name
|| !right_sym
)
2019 add_var_sym(&left_vsl
, left_name
, left_sym
);
2020 add_var_sym(&right_vsl
, right_name
, right_sym
);
2022 add_comparison_var_sym(left_name
, left_vsl
, op
, right_name
, right_vsl
);
2025 free_string(left_name
);
2026 free_string(right_name
);
2029 int param_compare_limit_is_impossible(struct expression
*expr
, int left_param
, char *left_key
, char *value
)
2031 struct smatch_state
*state
;
2032 char *left_name
= NULL
;
2033 char *right_name
= NULL
;
2034 struct symbol
*left_sym
, *right_sym
;
2035 struct expression
*left_arg
, *right_arg
;
2042 while (expr
->type
== EXPR_ASSIGNMENT
)
2043 expr
= strip_expr(expr
->right
);
2044 if (expr
->type
!= EXPR_CALL
)
2047 if (!split_op_param_key(value
, &op
, &right_param
, &right_key
))
2050 left_arg
= get_argument_from_call_expr(expr
->args
, left_param
);
2054 right_arg
= get_argument_from_call_expr(expr
->args
, right_param
);
2058 left_name
= get_variable_from_key(left_arg
, left_key
, &left_sym
);
2059 right_name
= get_variable_from_key(right_arg
, right_key
, &right_sym
);
2060 if (!left_name
|| !right_name
)
2063 snprintf(buf
, sizeof(buf
), "%s vs %s", left_name
, right_name
);
2064 state
= get_state(compare_id
, buf
, NULL
);
2067 state_op
= state_to_comparison(state
);
2071 if (!filter_comparison(remove_unsigned_from_comparison(state_op
), op
))
2074 free_string(left_name
);
2075 free_string(right_name
);
2079 static void free_data(struct symbol
*sym
)
2083 clear_compare_data_alloc();
2086 void register_comparison(int id
)
2089 add_hook(&save_start_states
, AFTER_DEF_HOOK
);
2090 add_unmatched_state_hook(compare_id
, unmatched_comparison
);
2091 add_merge_hook(compare_id
, &merge_compare_states
);
2092 add_hook(&free_data
, AFTER_FUNC_HOOK
);
2093 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
2094 add_split_return_callback(&print_return_comparison
);
2096 select_return_states_hook(PARAM_COMPARE
, &db_return_comparison
);
2099 void register_comparison_late(int id
)
2101 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
2104 void register_comparison_links(int id
)
2107 add_merge_hook(link_id
, &merge_links
);
2108 add_modification_hook(link_id
, &match_modify
);
2110 add_member_info_callback(link_id
, struct_member_callback
);
2113 void register_comparison_inc_dec(int id
)
2116 add_modification_hook_late(inc_dec_id
, &iter_modify
);
2119 void register_comparison_inc_dec_links(int id
)
2121 inc_dec_link_id
= id
;
2122 set_up_link_functions(inc_dec_id
, inc_dec_link_id
);
2125 static void filter_by_sm(struct sm_state
*sm
, int op
,
2126 struct state_list
**true_stack
,
2127 struct state_list
**false_stack
)
2129 struct compare_data
*data
;
2135 data
= sm
->state
->data
;
2138 filter_by_sm(sm
->left
, op
, true_stack
, false_stack
);
2139 filter_by_sm(sm
->right
, op
, true_stack
, false_stack
);
2144 if (data
->comparison
&&
2145 data
->comparison
== filter_comparison(data
->comparison
, op
))
2148 if (data
->comparison
&&
2149 data
->comparison
== filter_comparison(data
->comparison
, negate_comparison(op
)))
2153 add_ptr_list(true_stack
, sm
);
2155 add_ptr_list(false_stack
, sm
);
2158 filter_by_sm(sm
->left
, op
, true_stack
, false_stack
);
2159 filter_by_sm(sm
->right
, op
, true_stack
, false_stack
);
2163 struct sm_state
*comparison_implication_hook(struct expression
*expr
,
2164 struct state_list
**true_stack
,
2165 struct state_list
**false_stack
)
2167 struct sm_state
*sm
;
2170 static char buf
[256];
2172 if (expr
->type
!= EXPR_COMPARE
)
2177 left
= expr_to_var(expr
->left
);
2178 right
= expr_to_var(expr
->right
);
2179 if (!left
|| !right
) {
2185 if (strcmp(left
, right
) > 0) {
2190 op
= flip_comparison(op
);
2193 snprintf(buf
, sizeof(buf
), "%s vs %s", left
, right
);
2194 sm
= get_sm_state(compare_id
, buf
, NULL
);
2200 filter_by_sm(sm
, op
, true_stack
, false_stack
);
2201 if (!*true_stack
&& !*false_stack
)