netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Luciano Coelho <luciano.coelho@intel.com>
To: "Gustavo A. R. Silva" <gustavo@embeddedor.com>,
	Johannes Berg <johannes.berg@intel.com>,
	Emmanuel Grumbach <emmanuel.grumbach@intel.com>,
	Intel Linux Wireless <linuxwifi@intel.com>,
	Kalle Valo <kvalo@codeaurora.org>,
	"David S. Miller" <davem@davemloft.net>
Cc: linux-wireless@vger.kernel.org, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org, Kees Cook <keescook@chromium.org>
Subject: Re: [PATCH] iwlwifi: d3: use struct_size() in kzalloc()
Date: Tue, 04 Sep 2018 09:26:26 +0300	[thread overview]
Message-ID: <8771557a6d74a81b9d2bb32b0a1f65c811875c96.camel@intel.com> (raw)
In-Reply-To: <20180824011540.GA25716@embeddedor.com>

On Thu, 2018-08-23 at 20:15 -0500, Gustavo A. R. Silva wrote:
> One of the more common cases of allocation size calculations is finding
> the size of a structure that has a zero-sized array at the end, along
> with memory for some number of elements for that array. For example:
> 
> struct foo {
> 	int stuff;
>         void *entry[];
> };
> 
> instance = kzalloc(sizeof(struct foo) + sizeof(void *) * count, GFP_KERNEL);
> 
> Instead of leaving these open-coded and prone to type mistakes, we can
> now use the new struct_size() helper:
> 
> instance = kzalloc(struct_size(instance, entry, count), GFP_KERNEL);
> 
> This issue was detected with the help of Coccinelle.
> 
> Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
> ---
>  drivers/net/wireless/intel/iwlwifi/mvm/d3.c | 6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/net/wireless/intel/iwlwifi/mvm/d3.c b/drivers/net/wireless/intel/iwlwifi/mvm/d3.c
> index 79bdae9..6960318 100644
> --- a/drivers/net/wireless/intel/iwlwifi/mvm/d3.c
> +++ b/drivers/net/wireless/intel/iwlwifi/mvm/d3.c
> @@ -1769,8 +1769,7 @@ static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
>  		n_matches = 0;
>  	}
>  
> -	net_detect = kzalloc(sizeof(*net_detect) +
> -			     (n_matches * sizeof(net_detect->matches[0])),
> +	net_detect = kzalloc(struct_size(net_detect, matches, n_matches),
>  			     GFP_KERNEL);
>  	if (!net_detect || !n_matches)
>  		goto out_report_nd;
> @@ -1785,8 +1784,7 @@ static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
>  		for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; j++)
>  			n_channels += hweight8(fw_match->matching_channels[j]);
>  
> -		match = kzalloc(sizeof(*match) +
> -				(n_channels * sizeof(*match->channels)),
> +		match = kzalloc(struct_size(match, channels, n_channels),
>  				GFP_KERNEL);
>  		if (!match)
>  			goto out_report_nd;

Thanks! I applied this to our internal tree and it will reach the
mainline following our normal process.

--
Cheers,
Luca.

      parent reply	other threads:[~2018-09-04  6:26 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-24  1:15 [PATCH] iwlwifi: d3: use struct_size() in kzalloc() Gustavo A. R. Silva
2018-08-24  3:03 ` Kees Cook
2018-08-24  4:33   ` Joe Perches
2018-08-24 11:58     ` Gustavo A. R. Silva
2018-09-04  6:26 ` Luciano Coelho [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8771557a6d74a81b9d2bb32b0a1f65c811875c96.camel@intel.com \
    --to=luciano.coelho@intel.com \
    --cc=davem@davemloft.net \
    --cc=emmanuel.grumbach@intel.com \
    --cc=gustavo@embeddedor.com \
    --cc=johannes.berg@intel.com \
    --cc=keescook@chromium.org \
    --cc=kvalo@codeaurora.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-wireless@vger.kernel.org \
    --cc=linuxwifi@intel.com \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).