/* * 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 expression *prev; int comparison; 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; comparison = get_comparison(expr->left, expr->right); if (!comparison) return; if (show_special(comparison)[1] != '=') 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); }