All of lore.kernel.org
 help / color / mirror / Atom feed
From: Luca Coelho <luca@coelho.fi>
To: kvalo@codeaurora.org
Cc: linux-wireless@vger.kernel.org,
	"Gustavo A. R. Silva" <gustavo@embeddedor.com>,
	Luca Coelho <luciano.coelho@intel.com>
Subject: [PATCH 16/18] iwlwifi: mvm: d3: use struct_size() in kzalloc()
Date: Fri, 14 Dec 2018 22:29:43 +0200	[thread overview]
Message-ID: <20181214202945.32375-17-luca@coelho.fi> (raw)
In-Reply-To: <20181214202945.32375-1-luca@coelho.fi>

From: "Gustavo A. R. Silva" <gustavo@embeddedor.com>

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>
Signed-off-by: Luca Coelho <luciano.coelho@intel.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 9b018d00f78f..01b5338201d6 100644
--- a/drivers/net/wireless/intel/iwlwifi/mvm/d3.c
+++ b/drivers/net/wireless/intel/iwlwifi/mvm/d3.c
@@ -1811,8 +1811,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;
@@ -1827,8 +1826,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;
-- 
2.19.2


  parent reply	other threads:[~2018-12-14 20:30 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-14 20:29 [PATCH 00/18] iwlwifi: updates intended for v4.21 2018-12-14 Luca Coelho
2018-12-14 20:29 ` [PATCH 01/18] iwlwifi: pcie: use iwl_tx_cmd_gen2 and not iwl_tx_cmd Luca Coelho
2018-12-14 20:29 ` [PATCH 02/18] iwlwifi: mvm: remove dead code Luca Coelho
2018-12-14 20:29 ` [PATCH 03/18] iwlwifi: mvm: remove pointless NULL assignment Luca Coelho
2018-12-14 20:29 ` [PATCH 04/18] iwlwifi: mvm: cleanup iwl_mvm_tx_skb_non_sta Luca Coelho
2018-12-14 20:29 ` [PATCH 05/18] iwlwifi: mvm: fix setting HE ppe FW config Luca Coelho
2018-12-14 20:29 ` [PATCH 06/18] [BUGFIXiwlwifi: make MVM and DVM depend on MAC80211 Luca Coelho
2018-12-18 16:17   ` Kalle Valo
2018-12-19 10:24     ` Luca Coelho
2018-12-14 20:29 ` [PATCH 07/18] iwlwifi: split HE capabilities between AP and STA Luca Coelho
2018-12-14 20:29 ` [PATCH 08/18] iwlwifi: mvm: handle RX no data notification Luca Coelho
2018-12-14 20:29 ` [PATCH 09/18] iwlwifi: mvm: take station lock later in the code Luca Coelho
2018-12-14 20:29 ` [PATCH 10/18] iwlwifi: pcie: lock txq a bit later in reclaim code Luca Coelho
2018-12-14 20:29 ` [PATCH 11/18] iwlwifi: mvm: clean up SSN incrementation Luca Coelho
2018-12-14 20:29 ` [PATCH 12/18] iwlwifi: wrt: add rt status and num of rx/tx fifos to dump Luca Coelho
2018-12-14 20:29 ` [PATCH 13/18] iwlwifi: remove unused and wrong PHY_CFG_* macros Luca Coelho
2018-12-14 20:29 ` [PATCH 14/18] iwlwifi: mvm: set TWT responder capability bit in 11AX SoftAP mode Luca Coelho
2018-12-14 20:29 ` [PATCH 15/18] iwlwifi: mvm: fix spelling mistake "Recieved" -> "Received" Luca Coelho
2018-12-14 20:29 ` Luca Coelho [this message]
2018-12-14 20:29 ` [PATCH 17/18] iwlwifi: dbg: add debug data to warning Luca Coelho
2018-12-14 20:29 ` [PATCH 18/18] iwlwifi: bump the API version to 43 for 9000 and 22000 Luca Coelho

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=20181214202945.32375-17-luca@coelho.fi \
    --to=luca@coelho.fi \
    --cc=gustavo@embeddedor.com \
    --cc=kvalo@codeaurora.org \
    --cc=linux-wireless@vger.kernel.org \
    --cc=luciano.coelho@intel.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.