1 From 0cf83903aad03dc7f444a47990def48c4a9d3276 Mon Sep 17 00:00:00 2001
2 From: "Gustavo A. R. Silva" <gustavo@embeddedor.com>
3 Date: Wed, 3 Apr 2019 11:46:11 -0500
4 Subject: [PATCH] brcmfmac: Use struct_size() in kzalloc()
6 One of the more common cases of allocation size calculations is finding
7 the size of a structure that has a zero-sized array at the end, along
8 with memory for some number of elements for that array. For example:
15 size = sizeof(struct foo) + count * sizeof(struct boo);
16 instance = kzalloc(size, GFP_KERNEL)
18 Instead of leaving these open-coded and prone to type mistakes, we can
19 now use the new struct_size() helper:
21 instance = kzalloc(struct_size(instance, entry, count), GFP_KERNEL)
23 Notice that, in this case, variable reqsz is not necessary,
26 This code was detected with the help of Coccinelle.
28 Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
29 Signed-off-by: Kalle Valo <kvalo@codeaurora.org>
31 drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c | 4 +---
32 1 file changed, 1 insertion(+), 3 deletions(-)
34 --- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c
35 +++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c
36 @@ -711,7 +711,6 @@ brcmf_fw_alloc_request(u32 chip, u32 chi
42 for (i = 0; i < table_size; i++) {
43 if (mapping_table[i].chipid == chip &&
44 @@ -726,8 +725,7 @@ brcmf_fw_alloc_request(u32 chip, u32 chi
48 - reqsz = sizeof(*fwreq) + n_fwnames * sizeof(struct brcmf_fw_item);
49 - fwreq = kzalloc(reqsz, GFP_KERNEL);
50 + fwreq = kzalloc(struct_size(fwreq, items, n_fwnames), GFP_KERNEL);