/* * Copyright (C) 2022 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 */ #include "smatch.h" #include "smatch_extra.h" static int my_id; static void match_condition(struct expression *expr) { struct range_list *left_rl, *right_rl; struct expression *prev; sval_t sval; char *name; if (expr->type != EXPR_COMPARE) return; if (expr->op != '>' && expr->op != SPECIAL_UNSIGNED_GT) return; if (!get_implied_value(expr, &sval) || sval.value != 0) return; if (!get_implied_rl(expr->left, &left_rl) || !get_implied_rl(expr->right, &right_rl)) return; if (rl_max(left_rl).value != rl_min(right_rl).value) return; prev = get_assigned_expr(expr->left); prev = strip_expr(prev); if (!prev || prev->type != EXPR_CALL) return; name = expr_to_str(prev->fn); sm_warning("potential off by one check '%s()'", name); free_string(name); } void check_off_by_one_capped_return(int id) { my_id = id; add_hook(&match_condition, CONDITION_HOOK); }