2 * sparse/check_check_deref.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
11 * This is like check_deref_check.c except that it complains about code like:
16 * Of course, Smatch has complained about these for forever but the problem is
17 * the old scripts were too messy and complicated and generated too many false
20 * This check is supposed to be simpler because it only looks for one kind of
21 * null dereference bug instead of every kind. It also gets rid of the false
22 * positives caused by the checks that happen inside macros.
27 #include "smatch_slist.h"
28 #include "smatch_extra.h"
35 static void is_ok(const char *name
, struct symbol
*sym
, struct expression
*expr
, void *unused
)
37 set_state(my_id
, name
, sym
, &ok
);
40 static void check_dereference(struct expression
*expr
)
45 expr
= strip_expr(expr
);
46 if (getting_address())
48 sm
= get_sm_state_expr(my_id
, expr
);
51 if (is_ignored(my_id
, sm
->name
, sm
->sym
))
53 if (implied_not_equal(expr
, 0))
56 FOR_EACH_PTR(sm
->possible
, tmp
) {
57 if (tmp
->state
== &merged
)
59 if (tmp
->state
== &ok
)
61 if (tmp
->state
== &null
) {
62 sm_msg("error: we previously assumed '%s' could be null.", tmp
->name
);
63 add_ignore(my_id
, sm
->name
, sm
->sym
);
66 } END_FOR_EACH_PTR(tmp
);
69 static void match_dereferences(struct expression
*expr
)
71 if (expr
->type
!= EXPR_PREOP
)
73 check_dereference(expr
->unop
);
76 static void match_pointer_as_array(struct expression
*expr
)
80 check_dereference(expr
->unop
->left
);
83 static void match_condition(struct expression
*expr
)
85 if (get_macro_name(&expr
->pos
))
88 if (expr
->type
== EXPR_ASSIGNMENT
) {
89 match_condition(expr
->right
);
90 match_condition(expr
->left
);
92 set_true_false_states_expr(my_id
, expr
, &ok
, &null
);
95 void check_check_deref(int id
)
99 set_default_modification_hook(my_id
, &is_ok
);
100 add_hook(&match_dereferences
, DEREF_HOOK
);
101 add_hook(&match_pointer_as_array
, OP_HOOK
);
102 add_hook(&match_condition
, CONDITION_HOOK
);