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
23 #include "smatch_slist.h"
24 #include "smatch_extra.h"
33 static void ok_to_use(struct sm_state
*sm
, struct expression
*mod_expr
)
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
)
54 struct state_list
*slist
;
59 if (returns_unsigned(cur_func_sym
))
61 if (returns_pointer(cur_func_sym
))
63 if (!get_value(ret_value
, &sval
) || sval
.value
!= -1)
65 if (get_macro_name(ret_value
->pos
))
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");
75 } END_FOR_EACH_PTR(sm
);
81 void check_return_enomem(int id
)
83 if (option_project
!= PROJ_KERNEL
)
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
);