mac80211: brcm: backport brcmfmac 5.2 patches
[openwrt/openwrt.git] / package / kernel / mac80211 / patches / brcm / 366-v5.2-brcmfmac-Use-struct_size-in-kzalloc.patch
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()
5
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:
9
10 struct foo {
11 int stuff;
12 struct boo entry[];
13 };
14
15 size = sizeof(struct foo) + count * sizeof(struct boo);
16 instance = kzalloc(size, GFP_KERNEL)
17
18 Instead of leaving these open-coded and prone to type mistakes, we can
19 now use the new struct_size() helper:
20
21 instance = kzalloc(struct_size(instance, entry, count), GFP_KERNEL)
22
23 Notice that, in this case, variable reqsz is not necessary,
24 hence it is removed.
25
26 This code was detected with the help of Coccinelle.
27
28 Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
29 Signed-off-by: Kalle Valo <kvalo@codeaurora.org>
30 ---
31 drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c | 4 +---
32 1 file changed, 1 insertion(+), 3 deletions(-)
33
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
37 size_t mp_path_len;
38 u32 i, j;
39 char end = '\0';
40 - size_t reqsz;
41
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
45 return NULL;
46 }
47
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);
51 if (!fwreq)
52 return NULL;
53