locking: update to use stree
[smatch.git] / check_return_enomem.c
blob0853a3a2f05d6cc95c80c7f0249d1143d340ac01
1 /*
2 * Copyright (C) 2010 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
19 * Complains about places that return -1 instead of -ENOMEM
22 #include "smatch.h"
23 #include "smatch_slist.h"
24 #include "smatch_extra.h"
26 #define ENOMEM 12
28 static int my_id;
30 STATE(enomem);
31 STATE(ok);
33 static void ok_to_use(struct sm_state *sm, struct expression *mod_expr)
35 if (sm->state != &ok)
36 set_state(my_id, sm->name, sm->sym, &ok);
39 static void allocation_succeeded(const char *fn, struct expression *call_expr,
40 struct expression *assign_expr, void *unused)
42 set_state_expr(my_id, assign_expr->left, &ok);
45 static void allocation_failed(const char *fn, struct expression *call_expr,
46 struct expression *assign_expr, void *_arg_no)
48 set_state_expr(my_id, assign_expr->left, &enomem);
51 static void match_return(struct expression *ret_value)
53 struct sm_state *sm;
54 struct state_list *slist;
55 sval_t sval;
57 if (!ret_value)
58 return;
59 if (returns_unsigned(cur_func_sym))
60 return;
61 if (returns_pointer(cur_func_sym))
62 return;
63 if (!get_value(ret_value, &sval) || sval.value != -1)
64 return;
65 if (get_macro_name(ret_value->pos))
66 return;
68 slist = get_all_states(my_id);
70 FOR_EACH_PTR(slist, sm) {
71 if (sm->state == &enomem) {
72 sm_msg("warn: returning -1 instead of -ENOMEM is sloppy");
73 goto out;
75 } END_FOR_EACH_PTR(sm);
77 out:
78 free_slist(&slist);
81 void check_return_enomem(int id)
83 if (option_project != PROJ_KERNEL)
84 return;
86 my_id = id;
87 return_implies_state("kmalloc", valid_ptr_min, valid_ptr_max, &allocation_succeeded, INT_PTR(0));
88 return_implies_state("kmalloc", 0, 0, &allocation_failed, INT_PTR(0));
89 return_implies_state("kzalloc", valid_ptr_min, valid_ptr_max, &allocation_succeeded, INT_PTR(0));
90 return_implies_state("kzalloc", 0, 0, &allocation_failed, INT_PTR(0));
91 return_implies_state("kcalloc", valid_ptr_min, valid_ptr_max, &allocation_succeeded, INT_PTR(0));
92 return_implies_state("kcalloc", 0, 0, &allocation_failed, INT_PTR(0));
93 add_hook(&match_return, RETURN_HOOK);
94 add_modification_hook(my_id, &ok_to_use);