sval: handle casting to bool
[smatch.git] / check_deref_check.c
blob5d5f51446f323d838ab3064175988be351c88708
1 /*
2 * Copyright (C) 2009 Dan Carpenter.
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
18 #include "smatch.h"
19 #include "smatch_extra.h"
21 static int my_id;
23 STATE(derefed);
25 static void underef(struct sm_state *sm, struct expression *mod_expr)
27 set_state(my_id, sm->name, sm->sym, &undefined);
30 static void match_dereference(struct expression *expr)
32 if (__in_fake_assign)
33 return;
35 if (expr->type != EXPR_PREOP)
36 return;
37 expr = strip_expr(expr->unop);
38 if (!is_pointer(expr))
39 return;
40 if (implied_not_equal(expr, 0))
41 return;
43 if (is_impossible_path())
44 return;
46 set_state_expr(my_id, expr, &derefed);
49 static void set_param_dereferenced(struct expression *call, struct expression *arg, char *key, char *unused)
51 struct symbol *sym;
52 char *name;
54 name = get_variable_from_key(arg, key, &sym);
55 if (!name || !sym)
56 goto free;
58 if (implied_not_equal_name_sym(name, sym, 0))
59 goto free;
60 set_state(my_id, name, sym, &derefed);
62 free:
63 free_string(name);
66 static void match_condition(struct expression *expr)
68 struct sm_state *sm;
70 if (__in_pre_condition)
71 return;
73 if (get_macro_name(expr->pos))
74 return;
76 if (!is_pointer(expr))
77 return;
79 sm = get_sm_state_expr(my_id, expr);
80 if (!sm || sm->state != &derefed)
81 return;
83 sm_msg("warn: variable dereferenced before check '%s' (see line %d)", sm->name, sm->line);
84 set_state_expr(my_id, expr, &undefined);
87 void check_deref_check(int id)
89 my_id = id;
90 add_hook(&match_dereference, DEREF_HOOK);
91 add_hook(&match_condition, CONDITION_HOOK);
92 select_return_implies_hook(DEREFERENCE, &set_param_dereferenced);
93 add_modification_hook(my_id, &underef);