From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9F5E2C433F5 for ; Mon, 10 Sep 2018 18:26:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 4EAE92086A for ; Mon, 10 Sep 2018 18:26:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4EAE92086A Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728033AbeIJXVl (ORCPT ); Mon, 10 Sep 2018 19:21:41 -0400 Received: from mx2.suse.de ([195.135.220.15]:33740 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726603AbeIJXVl (ORCPT ); Mon, 10 Sep 2018 19:21:41 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 85515ACBF; Mon, 10 Sep 2018 18:26:18 +0000 (UTC) Date: Mon, 10 Sep 2018 20:26:18 +0200 Message-ID: From: Takashi Iwai To: "Rohit kumar" Cc: , , , , , Subject: Re: [PATCH] ASoC: Fix UBSAN warning at snd_soc_get/put_volsw_sx() In-Reply-To: <1536600836-12153-1-git-send-email-rohitkr@codeaurora.org> References: <1536600836-12153-1-git-send-email-rohitkr@codeaurora.org> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI/1.14.6 (Maruoka) FLIM/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL/10.8 Emacs/26 (x86_64-suse-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 10 Sep 2018 19:33:56 +0200, Rohit kumar wrote: > > In functions snd_soc_get_volsw_sx() or snd_soc_put_volsw_sx(), > if the result of (min + max) is negative, then fls() returns > signed integer with value as 32. This leads to signed integer > overflow as complete operation is considered as signed integer. > > UBSAN: Undefined behaviour in sound/soc/soc-ops.c:382:50 > signed integer overflow: > -2147483648 - 1 cannot be represented in type 'int' > Call trace: > [] __dump_stack lib/dump_stack.c:15 [inline] > [] dump_stack+0xec/0x158 lib/dump_stack.c:51 > [] ubsan_epilogue+0x18/0x50 lib/ubsan.c:164 > [] handle_overflow+0xf8/0x130 lib/ubsan.c:195 > [] __ubsan_handle_sub_overflow+0x34/0x44 lib/ubsan.c:211 > [] snd_soc_get_volsw_sx+0x1a8/0x1f8 sound/soc/soc-ops.c:382 > > Typecast the operation to unsigned int to fix the issue. > > Signed-off-by: Rohit kumar > --- > sound/soc/soc-ops.c | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/sound/soc/soc-ops.c b/sound/soc/soc-ops.c > index 592efb3..f8e3190 100644 > --- a/sound/soc/soc-ops.c > +++ b/sound/soc/soc-ops.c > @@ -373,7 +373,7 @@ int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol, > unsigned int rshift = mc->rshift; > int max = mc->max; > int min = mc->min; > - unsigned int mask = (1 << (fls(min + max) - 1)) - 1; > + unsigned int mask = ((unsigned int)(1 << (fls(min + max) - 1)) - 1); Cat it be simpler like below instead? unsigned int mask = (1U << (fls(min + max) - 1)) - 1; thanks, Takashi > unsigned int val; > int ret; > > @@ -418,7 +418,7 @@ int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol, > unsigned int rshift = mc->rshift; > int max = mc->max; > int min = mc->min; > - unsigned int mask = (1 << (fls(min + max) - 1)) - 1; > + unsigned int mask = ((unsigned int)(1 << (fls(min + max) - 1)) - 1); > int err = 0; > unsigned int val, val_mask, val2 = 0; > > -- > Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center, Inc., > is a member of Code Aurora Forum, a Linux Foundation Collaborative Project. > >