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=-10.0 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS 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 5B8FBC433E0 for ; Thu, 30 Jul 2020 08:42:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 374D0207F5 for ; Thu, 30 Jul 2020 08:42:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729122AbgG3ImB (ORCPT ); Thu, 30 Jul 2020 04:42:01 -0400 Received: from mx2.suse.de ([195.135.220.15]:42618 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729001AbgG3Ily (ORCPT ); Thu, 30 Jul 2020 04:41:54 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 5D930AE38; Thu, 30 Jul 2020 08:42:05 +0000 (UTC) Date: Thu, 30 Jul 2020 10:41:53 +0200 Message-ID: From: Takashi Iwai To: "Gustavo A. R. Silva" Cc: Jaroslav Kysela , Takashi Iwai , alsa-devel@alsa-project.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH][next] ALSA: vmaster: Use flex_array_size() helper in memcpy() In-Reply-To: <20200729221829.GA10623@embeddedor> References: <20200729221829.GA10623@embeddedor> 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/25.3 (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 Thu, 30 Jul 2020 00:18:29 +0200, Gustavo A. R. Silva wrote: > > Make use of the flex_array_size() helper to calculate the size of a > flexible array member within an enclosing structure. > > This helper offers defense-in-depth against potential integer overflows > and makes it explicitly clear that we are dealing with a flexible array > member. > > Signed-off-by: Gustavo A. R. Silva > --- > sound/core/vmaster.c | 3 ++- > 1 file changed, 2 insertions(+), 1 deletion(-) > > diff --git a/sound/core/vmaster.c b/sound/core/vmaster.c > index ab36f9898711..21ce4082cb5f 100644 > --- a/sound/core/vmaster.c > +++ b/sound/core/vmaster.c > @@ -262,7 +262,8 @@ int _snd_ctl_add_follower(struct snd_kcontrol *master, > return -ENOMEM; > srec->kctl = follower; > srec->follower = *follower; > - memcpy(srec->follower.vd, follower->vd, follower->count * sizeof(*follower->vd)); > + memcpy(srec->follower.vd, follower->vd, flex_array_size(srec, follower.vd, > + srec->follower.count)); Changing from follower->count to srec->follower.count isn't obvious, so it should have been mentioned in the log; other than that, the code change looks good. But since the API isn't in Linus tree yet, I'll postpone the merge until the API reaches to upstream. Maybe I can merge this during RC1 merge window, as those are trivial. BTW, looking at those patterns, I wonder whether it'd make sense to make the whole memset() call as a macro like safe_copy_array(srec->follower.vd, follower->vd, follower->count); ? thanks, Takashi