On Tue, Nov 15, 2022 at 11:00:49AM -0600, Alex Elder wrote: > > drivers/net/ipa/ipa_table.c > > 413 count = mem->size / sizeof(__le64); > > 414 hash_count = hash_mem && hash_mem->size / sizeof(__le64); > > Line 414 is wrong. It should be: > hash_count = hash_mem ? hash_mem->size / sizeof(__le64) : 0; > Heh. It really feels like this line should have generated a checker warning as well. I've created two versions. The first complains when ever there is a divide used as a condition: if (a / b) { The other complains when it's part of a logical && or ||. if (a && a / b) { drivers/net/ipa/ipa_table.c:414 ipa_table_init_add() warn: divide condition: 'hash_mem->size / 8' drivers/net/ipa/ipa_table.c:414 ipa_table_init_add() warn: divide condition (logical): 'hash_mem->size / 8' I'll test them out tonight and see if either gives useful results. regards, dan carpenter
/* * Copyright (C) 2022 Dan Carpenter. * * 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_condition1(struct expression *expr) { char *name; if (expr->type != EXPR_BINOP || expr->op != '/') return; name = expr_to_str(expr); sm_warning("divide condition: '%s'", name); free_string(name); } static void match_condition2(struct expression *expr) { struct expression *parent; char *name; if (expr->type != EXPR_BINOP || expr->op != '/') return; parent = expr_get_parent_expr(expr); while (parent && parent->type == EXPR_PREOP && parent->op == '(') parent = expr_get_parent_expr(parent); if (!parent || parent->type != EXPR_LOGICAL) return; name = expr_to_str(expr); sm_warning("divide condition (logical): '%s'", name); free_string(name); } void check_divide_condition(int id) { my_id = id; add_hook(&match_condition1, CONDITION_HOOK); add_hook(&match_condition2, CONDITION_HOOK); }