overflow: ignore "x = &foo[ARRAY_SIZE(foo)];"
[smatch.git] / check_deref_check.c
blob6e19b0d718d33c821fe7f59eca6b1d6e1d4799d5
1 /*
2 * sparse/check_deref_check.c
4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include "smatch.h"
11 #include "smatch_extra.h"
13 static int my_id;
15 STATE(derefed);
16 STATE(oktocheck);
18 static void underef(struct sm_state *sm)
20 if (sm->state == &derefed)
21 set_state(my_id, sm->name, sm->sym, &oktocheck);
24 static void match_dereference(struct expression *expr)
26 char *name;
28 if (expr->type != EXPR_PREOP)
29 return;
30 if (getting_address())
31 return;
32 expr = strip_expr(expr->unop);
33 if (implied_not_equal(expr, 0))
34 return;
36 set_state_expr(my_id, expr, &derefed);
37 name = get_variable_from_expr(expr, NULL);
38 if (!name)
39 return;
40 free_string(name);
43 static void set_param_dereferenced(struct expression *arg, char *unused)
45 set_state_expr(my_id, arg, &derefed);
48 static void match_condition(struct expression *expr)
50 struct sm_state *sm;
52 if (__in_pre_condition)
53 return;
55 if (get_macro_name(expr->pos))
56 return;
58 sm = get_sm_state_expr(my_id, expr);
59 if (!sm || sm->state != &derefed)
60 return;
62 sm_msg("warn: variable dereferenced before check '%s' (see line %d)", sm->name, sm->line);
63 set_state_expr(my_id, expr, &oktocheck);
66 void check_deref_check(int id)
68 my_id = id;
69 add_hook(&match_dereference, DEREF_HOOK);
70 add_hook(&match_condition, CONDITION_HOOK);
71 add_db_fn_call_callback(DEREFERENCE, &set_param_dereferenced);
72 add_modification_hook(my_id, &underef);