user_data, db: remove some false tagging as "user_data" (false positives)
[smatch.git] / smatch_extra.c
blob5c5ca3cf8923ed3d49c020510f8343c97e84b1d2
1 /*
2 * sparse/smatch_extra.c
4 * Copyright (C) 2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
11 * smatch_extra.c is supposed to track the value of every variable.
15 #include <stdlib.h>
16 #include <errno.h>
17 #ifndef __USE_ISOC99
18 #define __USE_ISOC99
19 #endif
20 #include <limits.h>
21 #include "parse.h"
22 #include "smatch.h"
23 #include "smatch_slist.h"
24 #include "smatch_extra.h"
26 static int my_id;
28 struct string_list *__ignored_macros = NULL;
29 static int in_warn_on_macro()
31 struct statement *stmt;
32 char *tmp;
33 char *macro;
35 stmt = get_current_statement();
36 if (!stmt)
37 return 0;
38 macro = get_macro_name(stmt->pos);
39 if (!macro)
40 return 0;
42 FOR_EACH_PTR(__ignored_macros, tmp) {
43 if (!strcmp(tmp, macro))
44 return 1;
45 } END_FOR_EACH_PTR(tmp);
46 return 0;
49 typedef void (mod_hook)(const char *name, struct symbol *sym, struct smatch_state *state);
50 DECLARE_PTR_LIST(void_fn_list, mod_hook *);
51 static struct void_fn_list *extra_mod_hooks;
52 void add_extra_mod_hook(mod_hook *fn)
54 mod_hook **p = malloc(sizeof(mod_hook *));
55 *p = fn;
56 add_ptr_list(&extra_mod_hooks, p);
59 void call_extra_mod_hooks(const char *name, struct symbol *sym, struct smatch_state *state)
61 mod_hook **fn;
63 FOR_EACH_PTR(extra_mod_hooks, fn) {
64 (*fn)(name, sym, state);
65 } END_FOR_EACH_PTR(fn);
68 struct sm_state *set_extra_mod(const char *name, struct symbol *sym, struct smatch_state *state)
70 if (in_warn_on_macro())
71 return NULL;
72 remove_from_equiv(name, sym);
73 call_extra_mod_hooks(name, sym, state);
74 return set_state(SMATCH_EXTRA, name, sym, state);
77 struct sm_state *set_extra_expr_mod(struct expression *expr, struct smatch_state *state)
79 struct symbol *sym;
80 char *name;
81 struct sm_state *ret = NULL;
83 expr = strip_expr(expr);
84 name = expr_to_var_sym(expr, &sym);
85 if (!name || !sym)
86 goto free;
87 ret = set_extra_mod(name, sym, state);
88 free:
89 free_string(name);
90 return ret;
93 void set_extra_nomod(const char *name, struct symbol *sym, struct smatch_state *state)
95 struct relation *rel;
96 struct smatch_state *orig_state;
98 orig_state = get_state(SMATCH_EXTRA, name, sym);
100 if (!estate_related(orig_state)) {
101 set_state(SMATCH_EXTRA, name, sym, state);
102 return;
105 set_related(state, estate_related(orig_state));
106 FOR_EACH_PTR(estate_related(orig_state), rel) {
107 if (option_debug_related)
108 sm_msg("updating related %s to %s", rel->name, state->name);
109 set_state(SMATCH_EXTRA, rel->name, rel->sym, state);
110 } END_FOR_EACH_PTR(rel);
114 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
116 void set_extra_expr_nomod(struct expression *expr, struct smatch_state *state)
118 char *name;
119 struct symbol *sym;
121 name = expr_to_var_sym(expr, &sym);
122 if (!name || !sym)
123 goto free;
124 set_extra_nomod(name, sym, state);
125 free:
126 free_string(name);
130 static void set_extra_true_false(const char *name, struct symbol *sym,
131 struct smatch_state *true_state,
132 struct smatch_state *false_state)
134 struct relation *rel;
135 struct smatch_state *orig_state;
137 if (in_warn_on_macro())
138 return;
140 orig_state = get_state(SMATCH_EXTRA, name, sym);
142 if (!estate_related(orig_state)) {
143 set_true_false_states(SMATCH_EXTRA, name, sym, true_state, false_state);
144 return;
147 if (true_state)
148 set_related(true_state, estate_related(orig_state));
149 if (false_state)
150 set_related(false_state, estate_related(orig_state));
152 FOR_EACH_PTR(estate_related(orig_state), rel) {
153 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
154 true_state, false_state);
155 } END_FOR_EACH_PTR(rel);
158 static void set_extra_expr_true_false(struct expression *expr,
159 struct smatch_state *true_state,
160 struct smatch_state *false_state)
162 char *name;
163 struct symbol *sym;
165 expr = strip_expr(expr);
166 name = expr_to_var_sym(expr, &sym);
167 if (!name || !sym)
168 goto free;
169 set_extra_true_false(name, sym, true_state, false_state);
170 free:
171 free_string(name);
174 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
176 struct expression *iter_var;
177 struct expression *condition;
178 struct sm_state *sm;
179 struct smatch_state *estate;
180 sval_t start;
182 condition = strip_expr(loop->iterator_pre_condition);
183 if (!condition)
184 return NULL;
185 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
186 return NULL;
187 if (condition->op != SPECIAL_DECREMENT)
188 return NULL;
190 iter_var = condition->unop;
191 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
192 if (!sm)
193 return NULL;
194 if (sval_cmp_val(estate_min(sm->state), 0) < 0)
195 return NULL;
196 start = estate_max(sm->state);
197 if (sval_cmp_val(start, 0) <= 0)
198 return NULL;
199 if (!sval_is_max(start))
200 start.value--;
202 if (condition->type == EXPR_PREOP) {
203 estate = alloc_estate_range(sval_type_val(start.type, 1), start);
204 if (estate_has_hard_max(sm->state))
205 estate_set_hard_max(estate);
206 estate_copy_fuzzy_max(estate, sm->state);
207 set_extra_expr_mod(iter_var, estate);
209 if (condition->type == EXPR_POSTOP) {
210 estate = alloc_estate_range(sval_type_val(start.type, 0), start);
211 if (estate_has_hard_max(sm->state))
212 estate_set_hard_max(estate);
213 estate_copy_fuzzy_max(estate, sm->state);
214 set_extra_expr_mod(iter_var, estate);
216 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
219 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
220 struct expression *condition)
222 struct expression *iter_var;
223 struct sm_state *sm;
224 struct smatch_state *estate;
225 sval_t start, end, max;
227 iter_var = iter_expr->unop;
228 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
229 if (!sm)
230 return NULL;
231 if (!estate_get_single_value(sm->state, &start))
232 return NULL;
233 if (get_implied_max(condition->right, &end))
234 end = sval_cast(get_type(iter_var), end);
235 else
236 end = sval_type_max(get_type(iter_var));
238 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
239 return NULL;
241 switch (condition->op) {
242 case SPECIAL_UNSIGNED_LT:
243 case SPECIAL_NOTEQUAL:
244 case '<':
245 if (!sval_is_min(end))
246 end.value--;
247 break;
248 case SPECIAL_UNSIGNED_LTE:
249 case SPECIAL_LTE:
250 break;
251 default:
252 return NULL;
254 if (sval_cmp(end, start) < 0)
255 return NULL;
256 estate = alloc_estate_range(start, end);
257 if (get_hard_max(condition->right, &max)) {
258 estate_set_hard_max(estate);
259 if (condition->op == '<' ||
260 condition->op == SPECIAL_UNSIGNED_LT ||
261 condition->op == SPECIAL_NOTEQUAL)
262 max.value--;
263 estate_set_fuzzy_max(estate, max);
265 set_extra_expr_mod(iter_var, estate);
266 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
269 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
270 struct expression *condition)
272 struct expression *iter_var;
273 struct sm_state *sm;
274 struct smatch_state *estate;
275 sval_t start, end;
277 iter_var = iter_expr->unop;
278 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
279 if (!sm)
280 return NULL;
281 if (!estate_get_single_value(sm->state, &start))
282 return NULL;
283 if (!get_implied_min(condition->right, &end))
284 end = sval_type_min(get_type(iter_var));
285 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
286 return NULL;
288 switch (condition->op) {
289 case SPECIAL_NOTEQUAL:
290 case '>':
291 if (!sval_is_min(end) && !sval_is_max(end))
292 end.value++;
293 break;
294 case SPECIAL_GTE:
295 break;
296 default:
297 return NULL;
299 if (sval_cmp(end, start) > 0)
300 return NULL;
301 estate = alloc_estate_range(end, start);
302 estate_set_hard_max(estate);
303 estate_set_fuzzy_max(estate, estate_get_fuzzy_max(estate));
304 set_extra_expr_mod(iter_var, estate);
305 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
308 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
310 struct expression *iter_expr;
311 struct expression *condition;
313 if (!loop->iterator_post_statement)
314 return NULL;
315 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
316 return NULL;
317 iter_expr = loop->iterator_post_statement->expression;
318 if (!loop->iterator_pre_condition)
319 return NULL;
320 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
321 return NULL;
322 condition = loop->iterator_pre_condition;
324 if (iter_expr->op == SPECIAL_INCREMENT)
325 return handle_canonical_for_inc(iter_expr, condition);
326 if (iter_expr->op == SPECIAL_DECREMENT)
327 return handle_canonical_for_dec(iter_expr, condition);
328 return NULL;
331 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct state_list **slist)
333 struct sm_state *ret;
335 __push_fake_cur_slist();
336 if (!loop->iterator_post_statement)
337 ret = handle_canonical_while_count_down(loop);
338 else
339 ret = handle_canonical_for_loops(loop);
340 *slist = __pop_fake_cur_slist();
341 return ret;
344 int __iterator_unchanged(struct sm_state *sm)
346 if (!sm)
347 return 0;
348 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
349 return 1;
350 return 0;
353 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
355 sval_t after_value;
357 /* paranoid checking. prolly not needed */
358 condition = strip_expr(condition);
359 if (!condition)
360 return;
361 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
362 return;
363 if (condition->op != SPECIAL_DECREMENT)
364 return;
365 after_value = estate_min(sm->state);
366 after_value.value--;
367 set_extra_mod(sm->name, sm->sym, alloc_estate_sval(after_value));
370 void __extra_pre_loop_hook_after(struct sm_state *sm,
371 struct statement *iterator,
372 struct expression *condition)
374 struct expression *iter_expr;
375 sval_t limit;
376 struct smatch_state *state;
378 if (!iterator) {
379 while_count_down_after(sm, condition);
380 return;
383 iter_expr = iterator->expression;
385 if (condition->type != EXPR_COMPARE)
386 return;
387 if (iter_expr->op == SPECIAL_INCREMENT) {
388 limit = sval_binop(estate_max(sm->state), '+',
389 sval_type_val(estate_type(sm->state), 1));
390 } else {
391 limit = sval_binop(estate_min(sm->state), '-',
392 sval_type_val(estate_type(sm->state), 1));
394 if (!estate_has_hard_max(sm->state) && !__has_breaks()) {
395 if (iter_expr->op == SPECIAL_INCREMENT)
396 state = alloc_estate_range(estate_min(sm->state), limit);
397 else
398 state = alloc_estate_range(limit, estate_max(sm->state));
399 } else {
400 state = alloc_estate_sval(limit);
402 if (!estate_has_hard_max(sm->state)) {
403 estate_clear_hard_max(state);
405 if (estate_has_fuzzy_max(sm->state)) {
406 sval_t hmax = estate_get_fuzzy_max(sm->state);
407 sval_t max = estate_max(sm->state);
409 if (sval_cmp(hmax, max) != 0)
410 estate_clear_fuzzy_max(state);
411 } else if (!estate_has_fuzzy_max(sm->state)) {
412 estate_clear_fuzzy_max(state);
415 set_extra_mod(sm->name, sm->sym, state);
418 static struct state_list *unmatched_slist;
419 static struct smatch_state *unmatched_state(struct sm_state *sm)
421 struct smatch_state *state;
423 if (unmatched_slist) {
424 state = get_state_slist(unmatched_slist, SMATCH_EXTRA, sm->name, sm->sym);
425 if (state)
426 return state;
428 return alloc_estate_whole(estate_type(sm->state));
431 static void clear_the_pointed_at(struct expression *expr, struct state_list *slist)
433 char *name;
434 struct symbol *sym;
435 struct sm_state *tmp;
437 name = expr_to_var_sym(expr, &sym);
438 if (!name || !sym)
439 goto free;
441 FOR_EACH_PTR(slist, tmp) {
442 if (tmp->name[0] != '*')
443 continue;
444 if (tmp->sym != sym)
445 continue;
446 if (strcmp(tmp->name + 1, name) != 0)
447 continue;
448 set_extra_mod(tmp->name, tmp->sym, alloc_estate_whole(estate_type(tmp->state)));
449 } END_FOR_EACH_PTR(tmp);
451 free:
452 free_string(name);
455 static void match_function_call(struct expression *expr)
457 struct expression *arg;
458 struct expression *tmp;
459 struct state_list *slist;
461 /* if we have the db this is handled in smatch_function_hooks.c */
462 if (!option_no_db)
463 return;
464 if (inlinable(expr->fn))
465 return;
467 slist = get_all_states(SMATCH_EXTRA);
469 FOR_EACH_PTR(expr->args, arg) {
470 tmp = strip_expr(arg);
471 if (tmp->type == EXPR_PREOP && tmp->op == '&')
472 set_extra_expr_mod(tmp->unop, alloc_estate_whole(get_type(tmp->unop)));
473 else
474 clear_the_pointed_at(tmp, slist);
475 } END_FOR_EACH_PTR(arg);
477 free_slist(&slist);
480 static int types_equiv_or_pointer(struct symbol *one, struct symbol *two)
482 if (!one || !two)
483 return 0;
484 if (one->type == SYM_PTR && two->type == SYM_PTR)
485 return 1;
486 return types_equiv(one, two);
489 static void match_vanilla_assign(struct expression *left, struct expression *right)
491 struct range_list *rl = NULL;
492 struct symbol *right_sym;
493 struct symbol *left_type;
494 struct symbol *right_type;
495 char *right_name = NULL;
496 struct symbol *sym;
497 char *name;
498 sval_t max;
499 struct smatch_state *state;
501 name = expr_to_var_sym(left, &sym);
502 if (!name)
503 return;
505 left_type = get_type(left);
506 right_type = get_type(right);
508 right_name = expr_to_var_sym(right, &right_sym);
509 if (right_name && right_sym &&
510 types_equiv_or_pointer(left_type, right_type)) {
511 set_equiv(left, right);
512 goto free;
515 if (get_implied_rl(right, &rl)) {
516 rl = cast_rl(left_type, rl);
517 state = alloc_estate_rl(rl);
518 if (get_hard_max(right, &max)) {
519 estate_set_hard_max(state);
520 estate_set_fuzzy_max(state, max);
522 } else {
523 rl = alloc_whole_rl(right_type);
524 rl = cast_rl(left_type, rl);
525 state = alloc_estate_rl(rl);
527 set_extra_mod(name, sym, state);
528 free:
529 free_string(right_name);
532 static int op_remove_assign(int op)
534 switch (op) {
535 case SPECIAL_ADD_ASSIGN:
536 return '+';
537 case SPECIAL_SUB_ASSIGN:
538 return '-';
539 case SPECIAL_MUL_ASSIGN:
540 return '*';
541 case SPECIAL_DIV_ASSIGN:
542 return '/';
543 case SPECIAL_MOD_ASSIGN:
544 return '%';
545 case SPECIAL_AND_ASSIGN:
546 return '&';
547 case SPECIAL_OR_ASSIGN:
548 return '|';
549 case SPECIAL_XOR_ASSIGN:
550 return '^';
551 case SPECIAL_SHL_ASSIGN:
552 return SPECIAL_LEFTSHIFT;
553 case SPECIAL_SHR_ASSIGN:
554 return SPECIAL_RIGHTSHIFT;
555 default:
556 return op;
560 static void match_assign(struct expression *expr)
562 struct range_list *rl = NULL;
563 struct expression *left;
564 struct expression *right;
565 struct expression *binop_expr;
566 struct symbol *left_type;
567 struct symbol *right_type;
568 struct symbol *sym;
569 char *name;
570 sval_t left_min, left_max;
571 sval_t right_min, right_max;
572 sval_t res_min, res_max;
574 left = strip_expr(expr->left);
576 right = strip_expr(expr->right);
577 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
578 right = strip_expr(right->left);
580 if (is_condition(expr->right))
581 return; /* handled in smatch_condition.c */
582 if (expr->op == '=' && right->type == EXPR_CALL)
583 return; /* handled in smatch_function_hooks.c */
584 if (expr->op == '=') {
585 match_vanilla_assign(left, right);
586 return;
589 name = expr_to_var_sym(left, &sym);
590 if (!name)
591 return;
593 left_type = get_type(left);
594 right_type = get_type(right);
596 res_min = sval_type_min(right_type);
597 res_max = sval_type_max(right_type);
599 switch (expr->op) {
600 case SPECIAL_ADD_ASSIGN:
601 get_absolute_max(left, &left_max);
602 get_absolute_max(right, &right_max);
603 if (sval_binop_overflows(left_max, '+', right_max))
604 break;
605 if (get_implied_min(left, &left_min) &&
606 !sval_is_negative_min(left_min) &&
607 get_implied_min(right, &right_min) &&
608 !sval_is_negative_min(right_min)) {
609 res_min = sval_binop(left_min, '+', right_min);
610 res_min = sval_cast(right_type, res_min);
612 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
613 break;
614 res_max = sval_binop(left_max, '+', right_max);
615 res_max = sval_cast(right_type, res_max);
616 break;
617 case SPECIAL_SUB_ASSIGN:
618 if (get_implied_max(left, &left_max) &&
619 !sval_is_max(left_max) &&
620 get_implied_min(right, &right_min) &&
621 !sval_is_min(right_min)) {
622 res_max = sval_binop(left_max, '-', right_min);
623 res_max = sval_cast(right_type, res_max);
625 if (inside_loop())
626 break;
627 if (get_implied_min(left, &left_min) &&
628 !sval_is_min(left_min) &&
629 get_implied_max(right, &right_max) &&
630 !sval_is_max(right_max)) {
631 res_min = sval_binop(left_min, '-', right_max);
632 res_min = sval_cast(right_type, res_min);
634 break;
635 case SPECIAL_AND_ASSIGN:
636 case SPECIAL_MOD_ASSIGN:
637 binop_expr = binop_expression(expr->left,
638 op_remove_assign(expr->op),
639 expr->right);
640 if (get_implied_rl(binop_expr, &rl)) {
641 rl = cast_rl(left_type, rl);
642 set_extra_mod(name, sym, alloc_estate_rl(rl));
643 goto free;
646 rl = cast_rl(left_type, alloc_rl(res_min, res_max));
647 set_extra_mod(name, sym, alloc_estate_rl(rl));
648 free:
649 free_string(name);
652 static struct smatch_state *increment_state(struct smatch_state *state)
654 sval_t min = estate_min(state);
655 sval_t max = estate_max(state);
657 if (!estate_rl(state))
658 return NULL;
660 if (inside_loop())
661 max = sval_type_max(max.type);
663 if (!sval_is_min(min) && !sval_is_max(min))
664 min.value++;
665 if (!sval_is_min(max) && !sval_is_max(max))
666 max.value++;
667 return alloc_estate_range(min, max);
670 static struct smatch_state *decrement_state(struct smatch_state *state)
672 sval_t min = estate_min(state);
673 sval_t max = estate_max(state);
675 if (!estate_rl(state))
676 return NULL;
678 if (inside_loop())
679 min = sval_type_min(min.type);
681 if (!sval_is_min(min) && !sval_is_max(min))
682 min.value--;
683 if (!sval_is_min(max) && !sval_is_max(max))
684 max.value--;
685 return alloc_estate_range(min, max);
688 static void unop_expr(struct expression *expr)
690 struct smatch_state *state;
692 if (expr->smatch_flags & Handled)
693 return;
695 switch (expr->op) {
696 case SPECIAL_INCREMENT:
697 state = get_state_expr(SMATCH_EXTRA, expr->unop);
698 state = increment_state(state);
699 if (!state)
700 state = alloc_estate_whole(get_type(expr));
701 set_extra_expr_mod(expr->unop, state);
702 break;
703 case SPECIAL_DECREMENT:
704 state = get_state_expr(SMATCH_EXTRA, expr->unop);
705 state = decrement_state(state);
706 if (!state)
707 state = alloc_estate_whole(get_type(expr));
708 set_extra_expr_mod(expr->unop, state);
709 break;
710 default:
711 return;
715 static void asm_expr(struct statement *stmt)
718 struct expression *expr;
719 struct symbol *type;
720 int state = 0;
722 FOR_EACH_PTR(stmt->asm_outputs, expr) {
723 switch (state) {
724 case 0: /* identifier */
725 case 1: /* constraint */
726 state++;
727 continue;
728 case 2: /* expression */
729 state = 0;
730 type = get_type(strip_expr(expr));
731 set_extra_expr_mod(expr, alloc_estate_whole(type));
732 continue;
734 } END_FOR_EACH_PTR(expr);
737 static void delete_state_tracker(struct tracker *t)
739 remove_from_equiv(t->name, t->sym);
740 delete_state(t->owner, t->name, t->sym);
743 static void scoped_state_extra(const char *name, struct symbol *sym)
745 struct tracker *t;
747 t = alloc_tracker(SMATCH_EXTRA, name, sym);
748 add_scope_hook((scope_hook *)&delete_state_tracker, t);
751 static void match_declarations(struct symbol *sym)
753 const char *name;
755 if (sym->ident) {
756 name = sym->ident->name;
757 if (!sym->initializer) {
758 set_state(SMATCH_EXTRA, name, sym, alloc_estate_whole(get_real_base_type(sym)));
759 scoped_state_extra(name, sym);
764 static void check_dereference(struct expression *expr)
766 if (outside_of_function())
767 return;
768 set_extra_expr_nomod(expr, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
771 static void match_dereferences(struct expression *expr)
773 if (expr->type != EXPR_PREOP)
774 return;
775 check_dereference(expr->unop);
778 static void match_pointer_as_array(struct expression *expr)
780 if (!is_array(expr))
781 return;
782 check_dereference(expr->unop->left);
785 static void set_param_dereferenced(struct expression *arg, char *unused)
787 check_dereference(arg);
790 static void match_function_def(struct symbol *sym)
792 struct symbol *arg;
794 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
795 if (!arg->ident)
796 continue;
797 set_state(my_id, arg->ident->name, arg, alloc_estate_whole(get_real_base_type(arg)));
798 } END_FOR_EACH_PTR(arg);
801 static sval_t add_one(sval_t sval)
803 sval.value++;
804 return sval;
807 static sval_t sub_one(sval_t sval)
809 sval.value--;
810 return sval;
813 static void handle_comparison(struct symbol *type, struct expression *left, int op, struct expression *right)
815 struct range_list *left_orig;
816 struct range_list *left_true;
817 struct range_list *left_false;
818 struct range_list *right_orig;
819 struct range_list *right_true;
820 struct range_list *right_false;
821 struct smatch_state *left_true_state;
822 struct smatch_state *left_false_state;
823 struct smatch_state *right_true_state;
824 struct smatch_state *right_false_state;
825 sval_t min, max, dummy, hard_max;
826 int left_postop = 0;
827 int right_postop = 0;
829 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
830 if (left->type == EXPR_POSTOP) {
831 left->smatch_flags |= Handled;
832 left_postop = left->op;
834 left = strip_expr(left->unop);
836 while (left->type == EXPR_ASSIGNMENT)
837 left = strip_expr(left->left);
839 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
840 if (right->type == EXPR_POSTOP) {
841 right->smatch_flags |= Handled;
842 right_postop = right->op;
844 right = strip_expr(right->unop);
847 if (get_implied_rl(left, &left_orig)) {
848 left_orig = cast_rl(type, left_orig);
849 } else {
850 min = sval_type_min(get_type(left));
851 max = sval_type_max(get_type(left));
852 left_orig = cast_rl(type, alloc_rl(min, max));
855 if (get_implied_rl(right, &right_orig)) {
856 right_orig = cast_rl(type, right_orig);
857 } else {
858 min = sval_type_min(get_type(right));
859 max = sval_type_max(get_type(right));
860 right_orig = cast_rl(type, alloc_rl(min, max));
862 min = sval_type_min(type);
863 max = sval_type_max(type);
865 left_true = clone_rl(left_orig);
866 left_false = clone_rl(left_orig);
867 right_true = clone_rl(right_orig);
868 right_false = clone_rl(right_orig);
870 switch (op) {
871 case '<':
872 case SPECIAL_UNSIGNED_LT:
873 left_true = remove_range(left_orig, rl_max(right_orig), max);
874 if (!sval_is_min(rl_min(right_orig))) {
875 left_false = remove_range(left_orig, min, sub_one(rl_min(right_orig)));
878 right_true = remove_range(right_orig, min, rl_min(left_orig));
879 if (!sval_is_max(rl_max(left_orig)))
880 right_false = remove_range(right_orig, add_one(rl_max(left_orig)), max);
881 break;
882 case SPECIAL_UNSIGNED_LTE:
883 case SPECIAL_LTE:
884 if (!sval_is_max(rl_max(right_orig)))
885 left_true = remove_range(left_orig, add_one(rl_max(right_orig)), max);
886 left_false = remove_range(left_orig, min, rl_min(right_orig));
888 if (!sval_is_min(rl_min(left_orig)))
889 right_true = remove_range(right_orig, min, sub_one(rl_min(left_orig)));
890 right_false = remove_range(right_orig, rl_max(left_orig), max);
892 if (sval_cmp(rl_min(left_orig), rl_min(right_orig)) == 0)
893 left_false = remove_range(left_false, rl_min(left_orig), rl_min(left_orig));
894 if (sval_cmp(rl_max(left_orig), rl_max(right_orig)) == 0)
895 right_false = remove_range(right_false, rl_max(left_orig), rl_max(left_orig));
896 break;
897 case SPECIAL_EQUAL:
898 if (!sval_is_max(rl_max(right_orig))) {
899 left_true = remove_range(left_true, add_one(rl_max(right_orig)), max);
901 if (!sval_is_min(rl_min(right_orig))) {
902 left_true = remove_range(left_true, min, sub_one(rl_min(right_orig)));
904 if (sval_cmp(rl_min(right_orig), rl_max(right_orig)) == 0)
905 left_false = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
907 if (!sval_is_max(rl_max(left_orig)))
908 right_true = remove_range(right_true, add_one(rl_max(left_orig)), max);
909 if (!sval_is_min(rl_min(left_orig)))
910 right_true = remove_range(right_true, min, sub_one(rl_min(left_orig)));
911 if (sval_cmp(rl_min(left_orig), rl_max(left_orig)) == 0)
912 right_false = remove_range(right_orig, rl_min(left_orig), rl_min(left_orig));
913 break;
914 case SPECIAL_UNSIGNED_GTE:
915 case SPECIAL_GTE:
916 if (!sval_is_min(rl_min(right_orig)))
917 left_true = remove_range(left_orig, min, sub_one(rl_min(right_orig)));
918 left_false = remove_range(left_orig, rl_max(right_orig), max);
920 if (!sval_is_max(rl_max(left_orig)))
921 right_true = remove_range(right_orig, add_one(rl_max(left_orig)), max);
922 right_false = remove_range(right_orig, min, rl_min(left_orig));
924 if (sval_cmp(rl_min(left_orig), rl_min(right_orig)) == 0)
925 right_false = remove_range(right_false, rl_min(left_orig), rl_min(left_orig));
926 if (sval_cmp(rl_max(left_orig), rl_max(right_orig)) == 0)
927 left_false = remove_range(left_false, rl_max(left_orig), rl_max(left_orig));
928 break;
929 case '>':
930 case SPECIAL_UNSIGNED_GT:
931 left_true = remove_range(left_orig, min, rl_min(right_orig));
932 if (!sval_is_max(rl_max(right_orig)))
933 left_false = remove_range(left_orig, add_one(rl_max(right_orig)), max);
935 right_true = remove_range(right_orig, rl_max(left_orig), max);
936 if (!sval_is_min(rl_min(left_orig)))
937 right_false = remove_range(right_orig, min, sub_one(rl_min(left_orig)));
938 break;
939 case SPECIAL_NOTEQUAL:
940 if (!sval_is_max(rl_max(right_orig)))
941 left_false = remove_range(left_false, add_one(rl_max(right_orig)), max);
942 if (!sval_is_min(rl_min(right_orig)))
943 left_false = remove_range(left_false, min, sub_one(rl_min(right_orig)));
944 if (sval_cmp(rl_min(right_orig), rl_max(right_orig)) == 0)
945 left_true = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
947 if (!sval_is_max(rl_max(left_orig)))
948 right_false = remove_range(right_false, add_one(rl_max(left_orig)), max);
949 if (!sval_is_min(rl_min(left_orig)))
950 right_false = remove_range(right_false, min, sub_one(rl_min(left_orig)));
951 if (sval_cmp(rl_min(left_orig), rl_max(left_orig)) == 0)
952 right_true = remove_range(right_orig, rl_min(left_orig), rl_min(left_orig));
953 break;
954 default:
955 return;
958 left_true = rl_truncate_cast(get_type(left), left_true);
959 left_false = rl_truncate_cast(get_type(left), left_false);
960 right_true = rl_truncate_cast(get_type(right), right_true);
961 right_false = rl_truncate_cast(get_type(right), right_false);
963 left_true_state = alloc_estate_rl(left_true);
964 left_false_state = alloc_estate_rl(left_false);
965 right_true_state = alloc_estate_rl(right_true);
966 right_false_state = alloc_estate_rl(right_false);
968 switch (op) {
969 case '<':
970 case SPECIAL_UNSIGNED_LT:
971 case SPECIAL_UNSIGNED_LTE:
972 case SPECIAL_LTE:
973 if (get_hard_max(right, &dummy))
974 estate_set_hard_max(left_true_state);
975 if (get_hard_max(left, &dummy))
976 estate_set_hard_max(right_false_state);
977 break;
978 case '>':
979 case SPECIAL_UNSIGNED_GT:
980 case SPECIAL_UNSIGNED_GTE:
981 case SPECIAL_GTE:
982 if (get_hard_max(left, &dummy))
983 estate_set_hard_max(right_true_state);
984 if (get_hard_max(right, &dummy))
985 estate_set_hard_max(left_false_state);
986 break;
989 switch (op) {
990 case '<':
991 case SPECIAL_UNSIGNED_LT:
992 case SPECIAL_UNSIGNED_LTE:
993 case SPECIAL_LTE:
994 if (get_hard_max(right, &hard_max)) {
995 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
996 hard_max.value--;
997 estate_set_fuzzy_max(left_true_state, hard_max);
999 if (get_implied_value(right, &hard_max)) {
1000 if (op == SPECIAL_UNSIGNED_LTE ||
1001 op == SPECIAL_LTE)
1002 hard_max.value++;
1003 estate_set_fuzzy_max(left_false_state, hard_max);
1005 if (get_hard_max(left, &hard_max)) {
1006 if (op == SPECIAL_UNSIGNED_LTE ||
1007 op == SPECIAL_LTE)
1008 hard_max.value--;
1009 estate_set_fuzzy_max(right_false_state, hard_max);
1011 if (get_implied_value(left, &hard_max)) {
1012 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1013 hard_max.value++;
1014 estate_set_fuzzy_max(right_true_state, hard_max);
1016 break;
1017 case '>':
1018 case SPECIAL_UNSIGNED_GT:
1019 case SPECIAL_UNSIGNED_GTE:
1020 case SPECIAL_GTE:
1021 if (get_hard_max(left, &hard_max)) {
1022 if (op == '>' || op == SPECIAL_UNSIGNED_GT)
1023 hard_max.value--;
1024 estate_set_fuzzy_max(right_true_state, hard_max);
1026 if (get_implied_value(left, &hard_max)) {
1027 if (op == SPECIAL_UNSIGNED_GTE ||
1028 op == SPECIAL_GTE)
1029 hard_max.value++;
1030 estate_set_fuzzy_max(right_false_state, hard_max);
1032 if (get_hard_max(right, &hard_max)) {
1033 if (op == SPECIAL_UNSIGNED_LTE ||
1034 op == SPECIAL_LTE)
1035 hard_max.value--;
1036 estate_set_fuzzy_max(left_false_state, hard_max);
1038 if (get_implied_value(right, &hard_max)) {
1039 if (op == '>' ||
1040 op == SPECIAL_UNSIGNED_GT)
1041 hard_max.value++;
1042 estate_set_fuzzy_max(left_true_state, hard_max);
1044 break;
1045 case SPECIAL_EQUAL:
1046 if (get_hard_max(left, &hard_max))
1047 estate_set_fuzzy_max(right_true_state, hard_max);
1048 if (get_hard_max(right, &hard_max))
1049 estate_set_fuzzy_max(left_true_state, hard_max);
1050 break;
1053 if (get_hard_max(left, &hard_max)) {
1054 estate_set_hard_max(left_true_state);
1055 estate_set_hard_max(left_false_state);
1057 if (get_hard_max(right, &hard_max)) {
1058 estate_set_hard_max(right_true_state);
1059 estate_set_hard_max(right_false_state);
1062 if (left_postop == SPECIAL_INCREMENT) {
1063 left_true_state = increment_state(left_true_state);
1064 left_false_state = increment_state(left_false_state);
1066 if (left_postop == SPECIAL_DECREMENT) {
1067 left_true_state = decrement_state(left_true_state);
1068 left_false_state = decrement_state(left_false_state);
1070 if (right_postop == SPECIAL_INCREMENT) {
1071 right_true_state = increment_state(right_true_state);
1072 right_false_state = increment_state(right_false_state);
1074 if (right_postop == SPECIAL_DECREMENT) {
1075 right_true_state = decrement_state(right_true_state);
1076 right_false_state = decrement_state(right_false_state);
1079 set_extra_expr_true_false(left, left_true_state, left_false_state);
1080 set_extra_expr_true_false(right, right_true_state, right_false_state);
1083 static int opposite_op(int op)
1085 switch (op) {
1086 case '+':
1087 return '-';
1088 case '-':
1089 return '+';
1091 return 0;
1094 static int is_simple_math(struct expression *expr)
1096 if (!expr)
1097 return 0;
1098 if (expr->type != EXPR_BINOP)
1099 return 0;
1100 if (!opposite_op(expr->op))
1101 return 0;
1102 return 1;
1105 static void move_known_values(struct expression **left_p, struct expression **right_p)
1107 struct expression *left = *left_p;
1108 struct expression *right = *right_p;
1109 sval_t sval;
1111 if (get_implied_value(left, &sval)) {
1112 if (!is_simple_math(right))
1113 return;
1114 if (right->op == '+' && get_value(right->left, &sval)) {
1115 *left_p = binop_expression(left, opposite_op(right->op), right->left);
1116 *right_p = right->right;
1117 return;
1119 if (get_value(right->right, &sval)) {
1120 *left_p = binop_expression(left, opposite_op(right->op), right->right);
1121 *right_p = right->left;
1122 return;
1124 return;
1126 if (get_implied_value(right, &sval)) {
1127 if (!is_simple_math(left))
1128 return;
1129 if (left->op == '+' && get_value(left->left, &sval)) {
1130 *right_p = binop_expression(right, opposite_op(left->op), left->left);
1131 *left_p = left->right;
1132 return;
1135 if (get_value(left->right, &sval)) {
1136 *right_p = binop_expression(right, opposite_op(left->op), left->right);
1137 *left_p = left->left;
1138 return;
1140 return;
1144 static int match_func_comparison(struct expression *expr)
1146 struct expression *left = strip_expr(expr->left);
1147 struct expression *right = strip_expr(expr->right);
1148 sval_t sval;
1151 * fixme: think about this harder. We should always be trying to limit
1152 * the non-call side as well. If we can't determine the limitter does
1153 * that mean we aren't querying the database and are missing important
1154 * information?
1157 if (left->type == EXPR_CALL) {
1158 if (get_implied_value(left, &sval)) {
1159 handle_comparison(get_type(expr), left, expr->op, right);
1160 return 1;
1162 if (!get_implied_value(right, &sval))
1163 return 1;
1164 function_comparison(expr->op, left, sval, 1);
1165 return 1;
1168 if (right->type == EXPR_CALL) {
1169 if (get_implied_value(right, &sval)) {
1170 handle_comparison(get_type(expr), left, expr->op, right);
1171 return 1;
1173 if (!get_implied_value(left, &sval))
1174 return 1;
1175 function_comparison(expr->op, right, sval, 0);
1176 return 1;
1179 return 0;
1182 static void match_comparison(struct expression *expr)
1184 struct expression *left_orig = strip_expr(expr->left);
1185 struct expression *right_orig = strip_expr(expr->right);
1186 struct expression *left, *right;
1187 struct expression *prev;
1188 struct symbol *type;
1190 if (match_func_comparison(expr))
1191 return;
1193 type = get_type(expr);
1194 if (!type)
1195 type = &llong_ctype;
1197 left = left_orig;
1198 right = right_orig;
1199 move_known_values(&left, &right);
1200 handle_comparison(type, left, expr->op, right);
1202 prev = get_assigned_expr(left_orig);
1203 if (is_simple_math(prev) && has_variable(prev, left_orig) == 0) {
1204 left = prev;
1205 right = right_orig;
1206 move_known_values(&left, &right);
1207 handle_comparison(type, left, expr->op, right);
1210 prev = get_assigned_expr(right_orig);
1211 if (is_simple_math(prev) && has_variable(prev, right_orig) == 0) {
1212 left = left_orig;
1213 right = prev;
1214 move_known_values(&left, &right);
1215 handle_comparison(type, left, expr->op, right);
1219 static void handle_AND_condition(struct expression *expr)
1221 struct range_list *rl = NULL;
1222 sval_t known;
1224 if (get_implied_value(expr->left, &known) && known.value > 0) {
1225 known.value--;
1226 get_absolute_rl(expr->right, &rl);
1227 rl = remove_range(rl, sval_type_val(known.type, 0), known);
1228 set_extra_expr_true_false(expr->right, alloc_estate_rl(rl), NULL);
1229 return;
1232 if (get_implied_value(expr->right, &known) && known.value > 0) {
1233 known.value--;
1234 get_absolute_rl(expr->left, &rl);
1235 rl = remove_range(rl, sval_type_val(known.type, 0), known);
1236 set_extra_expr_true_false(expr->left, alloc_estate_rl(rl), NULL);
1237 return;
1241 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1242 void __extra_match_condition(struct expression *expr)
1244 struct symbol *sym;
1245 char *name;
1246 struct smatch_state *pre_state;
1247 struct smatch_state *true_state;
1248 struct smatch_state *false_state;
1250 expr = strip_expr(expr);
1251 switch (expr->type) {
1252 case EXPR_CALL:
1253 function_comparison(SPECIAL_NOTEQUAL, expr, ll_to_sval(0), 1);
1254 return;
1255 case EXPR_PREOP:
1256 case EXPR_SYMBOL:
1257 case EXPR_DEREF: {
1258 sval_t zero;
1260 zero = sval_blank(expr);
1261 zero.value = 0;
1263 name = expr_to_var_sym(expr, &sym);
1264 if (!name)
1265 return;
1266 pre_state = get_state(my_id, name, sym);
1267 true_state = estate_filter_sval(pre_state, zero);
1268 if (possibly_true(expr, SPECIAL_EQUAL, zero_expr()))
1269 false_state = alloc_estate_sval(zero);
1270 else
1271 false_state = alloc_estate_empty();
1272 set_extra_true_false(name, sym, true_state, false_state);
1273 free_string(name);
1274 return;
1276 case EXPR_COMPARE:
1277 match_comparison(expr);
1278 return;
1279 case EXPR_ASSIGNMENT:
1280 __extra_match_condition(expr->left);
1281 return;
1282 case EXPR_BINOP:
1283 if (expr->op == '&')
1284 handle_AND_condition(expr);
1285 return;
1289 static void assume_indexes_are_valid(struct expression *expr)
1291 struct expression *array_expr;
1292 int array_size;
1293 struct expression *offset;
1294 struct symbol *offset_type;
1295 struct range_list *rl_before;
1296 struct range_list *rl_after;
1297 struct range_list *filter = NULL;
1298 sval_t size;
1300 expr = strip_expr(expr);
1301 if (!is_array(expr))
1302 return;
1304 offset = get_array_offset(expr);
1305 offset_type = get_type(offset);
1306 if (offset_type && type_signed(offset_type)) {
1307 filter = alloc_rl(sval_type_min(offset_type),
1308 sval_type_val(offset_type, -1));
1311 array_expr = strip_parens(expr->unop->left);
1312 array_size = get_real_array_size(array_expr);
1313 if (array_size > 1) {
1314 size = sval_type_val(offset_type, array_size);
1315 add_range(&filter, size, sval_type_max(offset_type));
1318 if (!filter)
1319 return;
1320 get_absolute_rl(offset, &rl_before);
1321 rl_after = rl_filter(rl_before, filter);
1322 if (rl_equiv(rl_before, rl_after))
1323 return;
1324 set_extra_expr_nomod(offset, alloc_estate_rl(rl_after));
1327 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
1328 int implied_not_equal(struct expression *expr, long long val)
1330 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
1333 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct smatch_state *state)
1335 if (estate_is_whole(state))
1336 return;
1337 sql_insert_caller_info(call, PARAM_VALUE, param, printed_name, state->name);
1338 if (estate_has_fuzzy_max(state))
1339 sql_insert_caller_info(call, FUZZY_MAX, param, printed_name,
1340 sval_to_str(estate_get_fuzzy_max(state)));
1343 static void db_limited_before(void)
1345 unmatched_slist = clone_slist(__get_cur_slist());
1348 static void db_limited_after(void)
1350 free_slist(&unmatched_slist);
1353 static void db_param_limit_filter(struct expression *expr, int param, char *key, char *value, int mod)
1355 struct expression *arg;
1356 char *name;
1357 struct symbol *sym;
1358 struct sm_state *sm;
1359 struct symbol *type;
1360 struct range_list *rl;
1361 struct range_list *limit;
1362 struct range_list *new;
1364 while (expr->type == EXPR_ASSIGNMENT)
1365 expr = strip_expr(expr->right);
1366 if (expr->type != EXPR_CALL)
1367 return;
1369 arg = get_argument_from_call_expr(expr->args, param);
1370 if (!arg)
1371 return;
1373 name = get_variable_from_key(arg, key, &sym);
1374 if (!name || !sym)
1375 goto free;
1377 sm = get_sm_state(SMATCH_EXTRA, name, sym);
1378 type = get_member_type_from_key(arg, key);
1379 if (sm)
1380 rl = estate_rl(sm->state);
1381 else
1382 rl = alloc_whole_rl(type);
1384 call_results_to_rl(expr, type, value, &limit);
1385 new = rl_intersection(rl, limit);
1387 /* We want to preserve the implications here */
1388 if (sm && rl_equiv(estate_rl(sm->state), new)) {
1389 __set_sm(sm);
1390 } else {
1391 if (mod)
1392 set_extra_mod(name, sym, alloc_estate_rl(new));
1393 else
1394 set_extra_nomod(name, sym, alloc_estate_rl(new));
1397 free:
1398 free_string(name);
1401 static void db_param_limit(struct expression *expr, int param, char *key, char *value)
1403 db_param_limit_filter(expr, param, key, value, 0);
1406 static void db_param_filter(struct expression *expr, int param, char *key, char *value)
1408 db_param_limit_filter(expr, param, key, value, 1);
1411 static void db_param_add(struct expression *expr, int param, char *key, char *value)
1413 struct expression *arg;
1414 char *name;
1415 struct symbol *sym;
1416 struct symbol *type;
1417 struct smatch_state *state;
1418 struct range_list *added = NULL;
1419 struct range_list *new;
1421 while (expr->type == EXPR_ASSIGNMENT)
1422 expr = strip_expr(expr->right);
1423 if (expr->type != EXPR_CALL)
1424 return;
1426 arg = get_argument_from_call_expr(expr->args, param);
1427 if (!arg)
1428 return;
1429 name = get_variable_from_key(arg, key, &sym);
1430 if (!name || !sym)
1431 goto free;
1433 type = get_member_type_from_key(arg, key);
1434 state = get_state(SMATCH_EXTRA, name, sym);
1435 if (state) {
1436 call_results_to_rl(expr, type, value, &added);
1437 new = rl_union(estate_rl(state), added);
1438 } else {
1439 new = alloc_whole_rl(type);
1442 set_extra_mod(name, sym, alloc_estate_rl(new));
1443 free:
1444 free_string(name);
1447 static void db_returned_member_info(struct expression *expr, int param, char *key, char *value)
1449 struct symbol *type;
1450 struct symbol *sym;
1451 char *name;
1452 struct range_list *rl;
1454 if (expr->type != EXPR_ASSIGNMENT)
1455 return;
1457 name = return_state_to_var_sym(expr, param, key, &sym);
1458 if (!name || !sym)
1459 goto free;
1460 type = get_member_type_from_key(expr->left, key);
1461 if (!type)
1462 return;
1463 call_results_to_rl(expr->right, type, value, &rl);
1464 set_extra_mod(name, sym, alloc_estate_rl(rl));
1466 free:
1467 free_string(name);
1470 static void returned_member_callback(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state)
1472 if (estate_is_whole(state))
1473 return;
1475 /* these are handled in smatch_param_filter/set/limit.c */
1476 if (printed_name[0] != '*' &&
1477 !strchr(printed_name, '.') && !strchr(printed_name, '-'))
1478 return;
1480 sql_insert_return_states(return_id, return_ranges, RETURN_VALUE, -1,
1481 printed_name, state->name);
1484 static void match_call_info(struct expression *expr)
1486 struct smatch_state *state;
1487 struct range_list *rl = NULL;
1488 struct expression *arg;
1489 struct symbol *type;
1490 int i = 0;
1492 FOR_EACH_PTR(expr->args, arg) {
1493 type = get_arg_type(expr->fn, i);
1495 if (get_implied_rl(arg, &rl))
1496 rl = cast_rl(type, rl);
1497 else
1498 rl = cast_rl(type, alloc_whole_rl(get_type(arg)));
1500 if (!is_whole_rl(rl))
1501 sql_insert_caller_info(expr, PARAM_VALUE, i, "$$", show_rl(rl));
1502 state = get_state_expr(SMATCH_EXTRA, arg);
1503 if (estate_has_fuzzy_max(state)) {
1504 sql_insert_caller_info(expr, FUZZY_MAX, i, "$$",
1505 sval_to_str(estate_get_fuzzy_max(state)));
1507 i++;
1508 } END_FOR_EACH_PTR(arg);
1511 static void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
1513 struct range_list *rl = NULL;
1514 struct smatch_state *state;
1515 struct symbol *type;
1516 char fullname[256];
1518 if (strcmp(key, "*$$") == 0)
1519 snprintf(fullname, sizeof(fullname), "*%s", name);
1520 else if (strncmp(key, "$$", 2) == 0)
1521 snprintf(fullname, 256, "%s%s", name, key + 2);
1522 else
1523 return;
1525 type = get_member_type_from_key(symbol_expression(sym), key);
1526 str_to_rl(type, value, &rl);
1527 state = alloc_estate_rl(rl);
1528 set_state(SMATCH_EXTRA, fullname, sym, state);
1531 static void set_param_hard_max(const char *name, struct symbol *sym, char *key, char *value)
1533 struct range_list *rl = NULL;
1534 struct smatch_state *state;
1535 struct symbol *type;
1536 char fullname[256];
1537 sval_t max;
1539 if (strcmp(key, "*$$") == 0)
1540 snprintf(fullname, sizeof(fullname), "*%s", name);
1541 else if (strncmp(key, "$$", 2) == 0)
1542 snprintf(fullname, 256, "%s%s", name, key + 2);
1543 else
1544 return;
1546 state = get_state(SMATCH_EXTRA, fullname, sym);
1547 if (!state)
1548 return;
1549 type = get_member_type_from_key(symbol_expression(sym), key);
1550 str_to_rl(type, value, &rl);
1551 if (!rl_to_sval(rl, &max))
1552 return;
1553 estate_set_fuzzy_max(state, max);
1556 void register_smatch_extra(int id)
1558 my_id = id;
1560 add_merge_hook(my_id, &merge_estates);
1561 add_unmatched_state_hook(my_id, &unmatched_state);
1562 add_hook(&match_function_def, FUNC_DEF_HOOK);
1563 add_hook(&match_declarations, DECLARATION_HOOK);
1564 select_caller_info_hook(set_param_value, PARAM_VALUE);
1565 select_caller_info_hook(set_param_hard_max, FUZZY_MAX);
1566 select_return_states_hook(RETURN_VALUE, &db_returned_member_info);
1567 select_return_states_before(&db_limited_before);
1568 select_return_states_hook(LIMITED_VALUE, &db_param_limit);
1569 select_return_states_hook(FILTER_VALUE, &db_param_filter);
1570 select_return_states_hook(ADDED_VALUE, &db_param_add);
1571 select_return_states_after(&db_limited_after);
1574 void register_smatch_extra_late(int id)
1576 add_hook(&match_dereferences, DEREF_HOOK);
1577 add_hook(&match_pointer_as_array, OP_HOOK);
1578 select_call_implies_hook(DEREFERENCE, &set_param_dereferenced);
1579 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
1580 add_hook(&match_assign, ASSIGNMENT_HOOK);
1581 add_hook(&unop_expr, OP_HOOK);
1582 add_hook(&asm_expr, ASM_HOOK);
1584 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1585 add_member_info_callback(my_id, struct_member_callback);
1586 add_returned_member_callback(my_id, returned_member_callback);
1588 add_hook(&assume_indexes_are_valid, OP_HOOK);