Commit 0cf83903 authored by Gustavo A. R. Silva's avatar Gustavo A. R. Silva Committed by Kalle Valo

brcmfmac: Use struct_size() in kzalloc()

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;
    struct boo entry[];
};

size = sizeof(struct foo) + count * sizeof(struct boo);
instance = kzalloc(size, 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)

Notice that, in this case, variable reqsz is not necessary,
hence it is removed.

This code was detected with the help of Coccinelle.
Signed-off-by: default avatarGustavo A. R. Silva <gustavo@embeddedor.com>
Signed-off-by: default avatarKalle Valo <kvalo@codeaurora.org>
parent 5f659c79
...@@ -711,7 +711,6 @@ brcmf_fw_alloc_request(u32 chip, u32 chiprev, ...@@ -711,7 +711,6 @@ brcmf_fw_alloc_request(u32 chip, u32 chiprev,
size_t mp_path_len; size_t mp_path_len;
u32 i, j; u32 i, j;
char end = '\0'; char end = '\0';
size_t reqsz;
for (i = 0; i < table_size; i++) { for (i = 0; i < table_size; i++) {
if (mapping_table[i].chipid == chip && if (mapping_table[i].chipid == chip &&
...@@ -726,8 +725,7 @@ brcmf_fw_alloc_request(u32 chip, u32 chiprev, ...@@ -726,8 +725,7 @@ brcmf_fw_alloc_request(u32 chip, u32 chiprev,
return NULL; return NULL;
} }
reqsz = sizeof(*fwreq) + n_fwnames * sizeof(struct brcmf_fw_item); fwreq = kzalloc(struct_size(fwreq, items, n_fwnames), GFP_KERNEL);
fwreq = kzalloc(reqsz, GFP_KERNEL);
if (!fwreq) if (!fwreq)
return NULL; return NULL;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment