366-v5.2-brcmfmac-Use-struct_size-in-kzalloc.patch 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. From 0cf83903aad03dc7f444a47990def48c4a9d3276 Mon Sep 17 00:00:00 2001
  2. From: "Gustavo A. R. Silva" <[email protected]>
  3. Date: Wed, 3 Apr 2019 11:46:11 -0500
  4. Subject: [PATCH] brcmfmac: Use struct_size() in kzalloc()
  5. One of the more common cases of allocation size calculations is finding
  6. the size of a structure that has a zero-sized array at the end, along
  7. with memory for some number of elements for that array. For example:
  8. struct foo {
  9. int stuff;
  10. struct boo entry[];
  11. };
  12. size = sizeof(struct foo) + count * sizeof(struct boo);
  13. instance = kzalloc(size, GFP_KERNEL)
  14. Instead of leaving these open-coded and prone to type mistakes, we can
  15. now use the new struct_size() helper:
  16. instance = kzalloc(struct_size(instance, entry, count), GFP_KERNEL)
  17. Notice that, in this case, variable reqsz is not necessary,
  18. hence it is removed.
  19. This code was detected with the help of Coccinelle.
  20. Signed-off-by: Gustavo A. R. Silva <[email protected]>
  21. Signed-off-by: Kalle Valo <[email protected]>
  22. ---
  23. drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c | 4 +---
  24. 1 file changed, 1 insertion(+), 3 deletions(-)
  25. --- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c
  26. +++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c
  27. @@ -711,7 +711,6 @@ brcmf_fw_alloc_request(u32 chip, u32 chi
  28. size_t mp_path_len;
  29. u32 i, j;
  30. char end = '\0';
  31. - size_t reqsz;
  32. for (i = 0; i < table_size; i++) {
  33. if (mapping_table[i].chipid == chip &&
  34. @@ -726,8 +725,7 @@ brcmf_fw_alloc_request(u32 chip, u32 chi
  35. return NULL;
  36. }
  37. - reqsz = sizeof(*fwreq) + n_fwnames * sizeof(struct brcmf_fw_item);
  38. - fwreq = kzalloc(reqsz, GFP_KERNEL);
  39. + fwreq = kzalloc(struct_size(fwreq, items, n_fwnames), GFP_KERNEL);
  40. if (!fwreq)
  41. return NULL;