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
19 #include "smatch_extra.h"
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
)
35 if (expr
->type
!= EXPR_PREOP
)
37 expr
= strip_expr(expr
->unop
);
38 if (!is_pointer(expr
))
40 if (implied_not_equal(expr
, 0))
43 if (is_impossible_path())
46 set_state_expr(my_id
, expr
, &derefed
);
49 static void set_param_dereferenced(struct expression
*call
, struct expression
*arg
, char *key
, char *unused
)
54 name
= get_variable_from_key(arg
, key
, &sym
);
60 if (implied_not_equal_name_sym(name
, sym
, 0))
62 set_state(my_id
, name
, sym
, &derefed
);
68 static void match_condition(struct expression
*expr
)
73 if (__in_pre_condition
)
76 name
= get_macro_name(expr
->pos
);
78 (strcmp(name
, "likely") != 0 && strcmp(name
, "unlikely") != 0))
81 if (!is_pointer(expr
))
84 sm
= get_sm_state_expr(my_id
, expr
);
85 if (!sm
|| sm
->state
!= &derefed
)
88 sm_warning("variable dereferenced before check '%s' (see line %d)", sm
->name
, sm
->line
);
89 set_state_expr(my_id
, expr
, &undefined
);
92 void check_deref_check(int id
)
95 add_hook(&match_dereference
, DEREF_HOOK
);
96 add_hook(&match_condition
, CONDITION_HOOK
);
97 select_return_implies_hook_early(DEREFERENCE
, &set_param_dereferenced
);
98 add_modification_hook(my_id
, &underef
);