Re: [PATCH] ASoC: qcom: q6dsp: Fix an off-by-one in q6adm_alloc_copp()

From: Dan Carpenter
Date: Thu Jul 21 2022 - 06:33:57 EST


On Thu, Jul 21, 2022 at 01:00:42PM +0300, Dan Carpenter wrote:
> sound/soc/qcom/qdsp6/q6adm.c:220 q6adm_alloc_copp() warn: impossible find_next_bit condition
>
> I'll probably try to make this check more generic

Attached is my first draft generic version. There are other ways I
could have written this, but I'll test my first draft and see what that
looks like.

sound/soc/qcom/qdsp6/q6adm.c:220 q6adm_alloc_copp() warn: potential off by one check 'find_first_zero_bit()'

regards,
dan carpenter

/*
* 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);
}