/* * 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" #include "smatch_slist.h" static int my_id; STATE(decremented); static struct expression *prev; static void match_condition(struct expression *expr) { struct expression *orig = expr; struct statement *stmt; struct range_list *rl; const char *iterator = ""; char *name; if (expr == prev) return; if (expr->type != EXPR_PREOP || expr->op != SPECIAL_DECREMENT) return; expr = strip_expr(expr->unop); get_absolute_rl(expr, &rl); if (rl_min(rl).value > 0) return; set_state_expr(my_id, expr, &decremented); stmt = get_parent_stmt(orig); if (stmt && stmt->type == STMT_ITERATOR) iterator = " (iterator)"; prev = orig; name = expr_to_str(expr); sm_warning("potential decrement underflow '%s' rl='%s'%s", name, show_rl(rl), iterator); free_string(name); } static void array_check(struct expression *expr) { struct expression *array, *offset; struct range_list *rl; char *name; expr = strip_expr(expr); array = get_array_base(expr); offset = get_array_offset(expr); if (!array || !offset) return; if (!get_state_expr(my_id, offset)) return; get_absolute_rl(offset, &rl); if (rl_min(rl).value >= 0) return; name = expr_to_str(offset); sm_warning("using underflowed offset '%s'", name); free_string(name); } void check_decrement_underflow(int id) { my_id = id; add_hook(&match_condition, CONDITION_HOOK); add_hook(&array_check, OP_HOOK); }