[PATCH 1/6] firmware-qemu_fw_cfg: Use kmalloc_array() in fw_cfg_register_dir_entries()

From: SF Markus Elfring
Date: Sun Sep 18 2016 - 08:51:23 EST


From: Markus Elfring <elfring@xxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 18 Sep 2016 09:39:31 +0200

* A multiplication for the size determination of a memory allocation
indicated that an array data structure should be processed.
Thus use the corresponding function "kmalloc_array".

This issue was detected by using the Coccinelle software.

* Replace the specification of a data structure by a pointer dereference
to make the corresponding size determination a bit safer according to
the Linux coding style convention.

* Delete the local variable "dir_size" which became unnecessary with
this refactoring.

Signed-off-by: Markus Elfring <elfring@xxxxxxxxxxxxxxxxxxxxx>
---
drivers/firmware/qemu_fw_cfg.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/drivers/firmware/qemu_fw_cfg.c b/drivers/firmware/qemu_fw_cfg.c
index 0e20116..e69653e 100644
--- a/drivers/firmware/qemu_fw_cfg.c
+++ b/drivers/firmware/qemu_fw_cfg.c
@@ -491,17 +491,17 @@ static int fw_cfg_register_dir_entries(void)
int ret = 0;
u32 count, i;
struct fw_cfg_file *dir;
- size_t dir_size;

fw_cfg_read_blob(FW_CFG_FILE_DIR, &count, 0, sizeof(count));
count = be32_to_cpu(count);
- dir_size = count * sizeof(struct fw_cfg_file);
-
- dir = kmalloc(dir_size, GFP_KERNEL);
+ dir = kmalloc_array(count, sizeof(*dir), GFP_KERNEL);
if (!dir)
return -ENOMEM;

- fw_cfg_read_blob(FW_CFG_FILE_DIR, dir, sizeof(count), dir_size);
+ fw_cfg_read_blob(FW_CFG_FILE_DIR,
+ dir,
+ sizeof(count),
+ sizeof(*dir) * count);

for (i = 0; i < count; i++) {
dir[i].size = be32_to_cpu(dir[i].size);
--
2.10.0