math: be more ambitious handling right shifts
[smatch.git] / check_return_enomem.c
blob8636c3619af8bcba5ca5bdd2dcc22289f3bb6b6e
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 stree *stree;
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 stree = __get_cur_stree();
70 FOR_EACH_MY_SM(my_id, stree, sm) {
71 if (sm->state == &enomem) {
72 sm_msg("warn: returning -1 instead of -ENOMEM is sloppy");
73 return;
75 } END_FOR_EACH_SM(sm);
78 void check_return_enomem(int id)
80 if (option_project != PROJ_KERNEL)
81 return;
83 my_id = id;
84 return_implies_state("kmalloc", valid_ptr_min, valid_ptr_max, &allocation_succeeded, INT_PTR(0));
85 return_implies_state("kmalloc", 0, 0, &allocation_failed, INT_PTR(0));
86 return_implies_state("kzalloc", valid_ptr_min, valid_ptr_max, &allocation_succeeded, INT_PTR(0));
87 return_implies_state("kzalloc", 0, 0, &allocation_failed, INT_PTR(0));
88 return_implies_state("kcalloc", valid_ptr_min, valid_ptr_max, &allocation_succeeded, INT_PTR(0));
89 return_implies_state("kcalloc", 0, 0, &allocation_failed, INT_PTR(0));
90 add_hook(&match_return, RETURN_HOOK);
91 add_modification_hook(my_id, &ok_to_use);