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"
28 static void match_return(struct expression
*ret_value
)
30 struct expression
*expr
;
37 if (returns_unsigned(cur_func_sym
))
39 if (returns_pointer(cur_func_sym
))
41 if (!get_value(ret_value
, &sval
) || sval
.value
!= -1)
43 if (get_macro_name(ret_value
->pos
))
46 stree
= __get_cur_stree();
48 FOR_EACH_MY_SM(SMATCH_EXTRA
, stree
, sm
) {
49 if (!estate_get_single_value(sm
->state
, &sval
) || sval
.value
!= 0)
51 expr
= get_assigned_expr_name_sym(sm
->name
, sm
->sym
);
54 if (expr
->type
!= EXPR_CALL
|| expr
->fn
->type
!= EXPR_SYMBOL
)
56 if (!expr
->fn
->symbol_name
&& !expr
->fn
->symbol_name
->name
)
58 /* To be honest the correct check is:
59 * if (strstr(expr->fn->symbol_name->name, "alloc"))
61 * But it generates too many warnings and it's too depressing.
63 if (strcmp(expr
->fn
->symbol_name
->name
, "kmalloc") != 0 &&
64 strcmp(expr
->fn
->symbol_name
->name
, "kzalloc") != 0)
67 sm_msg("warn: returning -1 instead of -ENOMEM is sloppy");
70 } END_FOR_EACH_SM(sm
);
73 void check_return_enomem(int id
)
75 if (option_project
!= PROJ_KERNEL
)
79 add_hook(&match_return
, RETURN_HOOK
);