2 * sparse/check_deref_check.c
4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
11 #include "smatch_extra.h"
17 static void underef(struct sm_state
*sm
)
19 set_state(my_id
, sm
->name
, sm
->sym
, &undefined
);
22 static void match_dereference(struct expression
*expr
)
24 if (expr
->type
!= EXPR_PREOP
)
26 if (getting_address())
28 expr
= strip_expr(expr
->unop
);
29 if (implied_not_equal(expr
, 0))
32 set_state_expr(my_id
, expr
, &derefed
);
35 static void set_param_dereferenced(struct expression
*arg
, char *unused
)
37 if (implied_not_equal(arg
, 0))
39 set_state_expr(my_id
, arg
, &derefed
);
42 static void match_condition(struct expression
*expr
)
46 if (__in_pre_condition
)
49 if (get_macro_name(expr
->pos
))
52 sm
= get_sm_state_expr(my_id
, expr
);
53 if (!sm
|| sm
->state
!= &derefed
)
56 sm_msg("warn: variable dereferenced before check '%s' (see line %d)", sm
->name
, sm
->line
);
57 set_state_expr(my_id
, expr
, &undefined
);
60 void check_deref_check(int id
)
63 add_hook(&match_dereference
, DEREF_HOOK
);
64 add_hook(&match_condition
, CONDITION_HOOK
);
65 add_db_fn_call_callback(DEREFERENCE
, &set_param_dereferenced
);
66 add_modification_hook(my_id
, &underef
);