Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1485789

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

From SF Markus Elfring <elfring@users.sourceforge.net>
Newsgroups linux.kernel
Subject [PATCH 1/6] firmware-qemu_fw_cfg: Use kmalloc_array() in fw_cfg_register_dir_entries()
Date 2016-09-18 15:00 +0200
Message-ID <siJRn-8eJ-23@gated-at.bofh.it> (permalink)
References <qEuGl-43C-5@gated-at.bofh.it> <siJHH-8bB-9@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


From: Markus Elfring <elfring@users.sourceforge.net>
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@users.sourceforge.net>
---
 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

Back to linux.kernel | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH 0/6] firmware-qemu_fw_cfg: Fine-tuning for four function  implementations SF Markus Elfring <elfring@users.sourceforge.net> - 2016-09-18 14:50 +0200
  [PATCH 3/6] firmware-qemu_fw_cfg: Rename jump labels in  fw_cfg_register_file() SF Markus Elfring <elfring@users.sourceforge.net> - 2016-09-18 15:00 +0200
  [PATCH 6/6] firmware-qemu_fw_cfg: Move a variable assignment in  fw_cfg_sysfs_probe() SF Markus Elfring <elfring@users.sourceforge.net> - 2016-09-18 15:00 +0200
  [PATCH 1/6] firmware-qemu_fw_cfg: Use kmalloc_array() in  fw_cfg_register_dir_entries() SF Markus Elfring <elfring@users.sourceforge.net> - 2016-09-18 15:00 +0200
  [PATCH 2/6] firmware-qemu_fw_cfg: Improve a size determination in  fw_cfg_register_file() SF Markus Elfring <elfring@users.sourceforge.net> - 2016-09-18 15:00 +0200
  [PATCH 4/6] firmware-qemu_fw_cfg: Improve a size determination in  fw_cfg_build_symlink() SF Markus Elfring <elfring@users.sourceforge.net> - 2016-09-18 15:00 +0200
  [PATCH 5/6] firmware-qemu_fw_cfg: Rename jump labels in  fw_cfg_sysfs_probe() SF Markus Elfring <elfring@users.sourceforge.net> - 2016-09-18 15:00 +0200
  Re: [PATCH 0/6] firmware-qemu_fw_cfg: Fine-tuning for four function  implementations "Gabriel L. Somlo" <somlo@cmu.edu> - 2016-09-20 13:40 +0200

csiph-web