From f65a092d16cf269d67797559d94f8e3a0f13f09d Mon Sep 17 00:00:00 2001 From: Dan Carpenter Date: Wed, 18 Feb 2015 12:06:18 +0300 Subject: [PATCH] *new* return_cast: complain about return -EINVAL in u8 functions The kernel returns negative error codes, normally. Some people forget this and declare their functions as returning unsigned char so the return code is truncated. Signed-off-by: Dan Carpenter --- check_list.h | 1 + check_return_cast.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 check_return_cast.c diff --git a/check_list.h b/check_list.h index ae78b6b7..ad50459c 100644 --- a/check_list.h +++ b/check_list.h @@ -97,6 +97,7 @@ CK(check_pointer_math) CK(check_bit_shift) CK(check_macro_side_effects) CK(check_sizeof) +CK(check_return_cast) CK(check_or_vs_and) CK(check_passes_sizeof) CK(check_assign_vs_compare) diff --git a/check_return_cast.c b/check_return_cast.c new file mode 100644 index 00000000..d6eca9eb --- /dev/null +++ b/check_return_cast.c @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2012 Oracle. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt + */ + +/* + * Complains about places that return -1 instead of -ENOMEM + */ + +#include "smatch.h" + +static int my_id; + +static void match_return(struct expression *ret_value) +{ + struct symbol *func_type = get_real_base_type(cur_func_sym); + sval_t sval; + + if (!func_type) + return; + if (!type_unsigned(func_type)) + return; + if (type_bits(func_type) > 16) + return; + if (!get_fuzzy_min(ret_value, &sval)) + return; + if (sval_is_positive(sval) || sval_cmp_val(sval, -1) == 0) + return; + + sm_msg("warn: signedness bug returning '%s'", sval_to_str(sval)); +} + +void check_return_cast(int id) +{ + my_id = id; + add_hook(&match_return, RETURN_HOOK); +} -- 2.11.4.GIT