precedence: cut down a few false positives
[smatch.git] / check_deref.c
blobf950fe232153e6adaf82b0d0c85a745892beb8ca
1 /*
2 * sparse/check_deref.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
11 * There was a previous null dereference test but it was too confusing and
12 * difficult to debug. This test is much simpler in its goals and scope.
14 * This test only complains about:
15 * 1) dereferencing uninitialized variables
16 * 2) dereferencing variables which were assigned as null.
17 * 3) dereferencing variables which were assigned a function the returns
18 * null.
20 * If we dereference something then we complain if any of those three
21 * are possible.
25 #include "smatch.h"
26 #include "smatch_slist.h"
28 static int my_id;
30 STATE(null);
31 STATE(ok);
32 STATE(uninitialized);
34 static struct smatch_state *alloc_my_state(const char *name)
36 struct smatch_state *state;
38 state = malloc(sizeof(*state));
39 state->name = name;
40 return state;
43 static struct smatch_state *unmatched_state(struct sm_state *sm)
45 return &ok;
48 static void is_ok(const char *name, struct symbol *sym, struct expression *expr, void *unused)
50 set_state(my_id, name, sym, &ok);
53 static void match_dereferences(struct expression *expr)
55 struct sm_state *sm;
56 struct sm_state *tmp;
58 if (expr->type != EXPR_PREOP)
59 return;
60 expr = strip_expr(expr->unop);
61 sm = get_sm_state_expr(my_id, expr);
62 if (!sm)
63 return;
64 if (is_ignored(my_id, sm->name, sm->sym))
65 return;
67 FOR_EACH_PTR(sm->possible, tmp) {
68 if (tmp->state == &merged)
69 continue;
70 if (tmp->state == &ok)
71 continue;
72 add_ignore(my_id, sm->name, sm->sym);
73 if (tmp->state == &null) {
74 sm_msg("error: potential null derefence '%s'.", tmp->name);
75 return;
77 if (tmp->state == &uninitialized) {
78 sm_msg("error: potentially derefencing uninitialized '%s'.", tmp->name);
79 return;
81 sm_msg("error: potential null dereference '%s'. (%s returns null)",
82 tmp->name, tmp->state->name);
83 return;
84 } END_FOR_EACH_PTR(tmp);
87 static void match_declarations(struct symbol *sym)
89 const char *name;
91 if ((get_base_type(sym))->type == SYM_ARRAY)
92 return;
94 name = sym->ident->name;
95 if (!sym->initializer) {
96 set_state(my_id, name, sym, &uninitialized);
97 scoped_state(my_id, name, sym);
101 static void match_assign(struct expression *expr)
103 if (is_zero(expr->right)) {
104 set_state_expr(my_id, expr->left, &null);
105 return;
109 static void match_condition(struct expression *expr)
111 if (expr->type == EXPR_ASSIGNMENT) {
112 match_condition(expr->right);
113 match_condition(expr->left);
115 if (!get_state_expr(my_id, expr))
116 return;
117 set_true_false_states_expr(my_id, expr, &ok, NULL);
120 static void match_assign_returns_null(const char *fn, struct expression *expr, void *unused)
122 struct smatch_state *state;
124 state = alloc_my_state(fn);
125 set_state_expr(my_id, expr->left, state);
128 static void register_allocation_funcs(void)
130 struct token *token;
131 const char *func;
133 token = get_tokens_file("kernel.allocation_funcs");
134 if (!token)
135 return;
136 if (token_type(token) != TOKEN_STREAMBEGIN)
137 return;
138 token = token->next;
139 while (token_type(token) != TOKEN_STREAMEND) {
140 if (token_type(token) != TOKEN_IDENT)
141 return;
142 func = show_ident(token->ident);
143 add_function_assign_hook(func, &match_assign_returns_null, NULL);
144 token = token->next;
146 clear_token_alloc();
149 void check_deref(int id)
151 my_id = id;
153 add_unmatched_state_hook(my_id, &unmatched_state);
154 set_default_modification_hook(my_id, &is_ok);
156 add_hook(&match_dereferences, DEREF_HOOK);
157 add_hook(&match_condition, CONDITION_HOOK);
158 add_hook(&match_declarations, DECLARATION_HOOK);
159 add_hook(&match_assign, ASSIGNMENT_HOOK);
160 if (option_project == PROJ_KERNEL)
161 register_allocation_funcs();