db: move an assignment outside the loop
[smatch.git] / check_deref_check.c
blob32c6441b7c63c36f6f6714c097e652b5d3321b13
1 /*
2 * Copyright (C) 2009 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
18 #include "smatch.h"
19 #include "smatch_slist.h"
20 #include "smatch_extra.h"
22 static int my_id;
24 STATE(derefed);
26 static void underef(struct sm_state *sm, struct expression *mod_expr)
28 set_state(my_id, sm->name, sm->sym, &undefined);
31 static void deref_hook(struct expression *expr)
33 if (implied_not_equal(expr, 0))
34 return;
35 if (is_impossible_path())
36 return;
37 set_state_expr(my_id, expr, &derefed);
40 static void match_condition(struct expression *expr)
42 struct sm_state *sm;
43 char *name;
45 if (__in_pre_condition)
46 return;
48 name = get_macro_name(expr->pos);
49 if (name &&
50 (strcmp(name, "likely") != 0 && strcmp(name, "unlikely") != 0))
51 return;
53 if (!is_pointer(expr))
54 return;
56 sm = get_sm_state_expr(my_id, expr);
57 if (!sm || sm->state != &derefed)
58 return;
60 sm_warning("variable dereferenced before check '%s' (see line %d)", sm->name, sm->line);
61 set_state_expr(my_id, expr, &undefined);
64 static void match_err_check(struct expression *expr, const char *name, struct symbol *sym, void *data)
66 struct sm_state *sm;
67 char *macro;
69 if (__in_pre_condition)
70 return;
72 sm = get_sm_state(my_id, name, sym);
73 if (!sm || sm->state != &derefed)
74 return;
76 macro = get_macro_name(expr->pos);
77 if (macro && strcmp(macro, "gvt_vgpu_err") == 0)
78 return;
80 sm_warning("variable dereferenced before IS_ERR check '%s' (see line %d)", sm->name, sm->line);
81 set_state_expr(my_id, expr, &undefined);
84 void check_deref_check(int id)
86 my_id = id;
88 add_dereference_hook(deref_hook);
89 add_modification_hook(my_id, &underef);
91 add_hook(&match_condition, CONDITION_HOOK);
93 add_function_param_key_hook("IS_ERR", &match_err_check, 0, "$", NULL);
94 add_function_param_key_hook("PTR_ERR_OR_ZERO", &match_err_check, 0, "$", NULL);
95 add_function_param_key_hook("IS_ERR_OR_NULL", &match_err_check, 0, "$", NULL);