/* * Copyright (C) 2021 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_slist.h" #include "smatch_extra.h" static int my_id; static bool is_plus(struct expression *expr) { if (expr->type == EXPR_BINOP && expr->op == '+') return true; /* fixme: what about dst += snprintf() */ return false; } static void match_snprintf(const char *fn, struct expression *expr, void *unused) { struct expression *dest, *limit; char *name; dest = get_argument_from_call_expr(expr->args, 0); limit = get_argument_from_call_expr(expr->args, 1); dest = strip_expr(dest); limit = strip_expr(limit); if (!dest || !limit) return; if (!is_plus(dest)) return; if (limit->type == EXPR_BINOP && limit->op == '-') return; name = expr_to_str(limit); sm_warning("expected subtract in snprintf limit '%s'", name); free_string(name); } void check_snprintf_no_minus(int id) { my_id = id; add_function_hook("snprintf", &match_snprintf, NULL); add_function_hook("scnprintf", &match_snprintf, NULL); }