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"
18 static void underef(const char *name
, struct symbol
*sym
, struct expression
*expr
, void *unused
)
20 set_state(my_id
, name
, sym
, &oktocheck
);
23 static void match_dereference(struct expression
*expr
)
27 if (expr
->type
!= EXPR_PREOP
)
29 if (getting_address())
32 expr
= strip_expr(expr
->unop
);
33 if (implied_not_equal(expr
, 0))
35 set_state_expr(my_id
, expr
, &derefed
);
36 name
= get_variable_from_expr(expr
, NULL
);
39 add_modification_hook(my_id
, name
, &underef
, NULL
);
43 static void set_param_dereferenced(struct expression
*arg
, char *unused
)
45 set_state_expr(my_id
, arg
, &derefed
);
46 add_modification_hook_expr(my_id
, arg
, &underef
, NULL
);
49 static void match_condition(struct expression
*expr
)
53 if (__in_pre_condition
)
56 if (get_macro_name(expr
->pos
))
59 sm
= get_sm_state_expr(my_id
, expr
);
60 if (!sm
|| sm
->state
!= &derefed
)
62 if (implied_not_equal(expr
, 0))
65 sm_msg("warn: variable dereferenced before check '%s' (see line %d)", sm
->name
, sm
->line
);
66 set_state_expr(my_id
, expr
, &oktocheck
);
69 void check_deref_check(int id
)
72 add_hook(&match_dereference
, DEREF_HOOK
);
73 add_hook(&match_condition
, CONDITION_HOOK
);
74 add_db_fn_call_callback(DEREFERENCE
, &set_param_dereferenced
);