Skip to contentSkip to conversation

scsi: Fix the name string memory leak during scsi scan

PATCH In review Bin Meng 3 participants 3 messages Nov 5, 2025 – Nov 11, 2025 SCSI

lore
Share & tools
lore

Bin Meng

author
#0 lore raw

PATCH 1/1 scsi: Fix the name string memory leak during scsi scan

There is a memory leak during the scsi scan process due to the strdup'ed name string is never freed. Actually it is unnecessary to pass a strdup'ed name string to blk_create_devicef() as we can use the name string on the stack directly.

---

 drivers/scsi/scsi.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/scsi/scsi.c +2 -5
index 05608399be1..b414d022f3f 100644
@@ -584,7 +584,7 @@ static int do_scsi_scan_one(struct udevice *dev, int id, int lun, bool verbose)
 	struct udevice *bdev;
 	struct blk_desc bd;
 	struct blk_desc *bdesc;
-	char str[10], *name;
+	char str[10];
 
 	/*
 	 * detect the scsi driver to get information about its geometry (block
@@ -600,10 +600,7 @@ static int do_scsi_scan_one(struct udevice *dev, int id, int lun, bool verbose)
 	* block devices created
 	*/
 	snprintf(str, sizeof(str), "id%dlun%d", id, lun);
-	name = strdup(str);
-	if (!name)
-		return log_msg_ret("nam", -ENOMEM);
-	ret = blk_create_devicef(dev, "scsi_blk", name, UCLASS_SCSI, -1,
+	ret = blk_create_devicef(dev, "scsi_blk", str, UCLASS_SCSI, -1,
 				 bd.blksz, bd.lba, &bdev);
 	if (ret) {
 		debug("Can't create device\n");
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 25 lines · Bin Meng

There is a memory leak during the scsi scan process due to the strdup'ed name string is never freed. Actually it is unnecessary to pass a strdup'ed name string to blk_create_devicef() as we can use the name string on the stack directly.

---

  drivers/scsi/scsi.c | 7 ++-----
  1 file changed, 2 insertions(+), 5 deletions(-)
drivers/scsi/scsi.c +1 -1
index 05608399be1..b414d022f3f 100644
@@ -584,7 +584,7 @@ static int do_scsi_scan_one(struct udevice *dev, int id, int lun, bool verbose)
  	struct udevice *bdev;
  	struct blk_desc bd;
  	struct blk_desc *bdesc;
-	char str[10], *name;
+	char str[10];
 
  	/*
  	 * detect the scsi driver to get information about its geometry (block

drivers/scsi/scsi.c static int do_scsi_scan_one(struct udevice *dev, int id, int lun, bool verbose) · line 603

Quoted 8 lines
drivers/scsi/scsi.c +1 -4
@@ -600,10 +600,7 @@ static int do_scsi_scan_one(struct udevice *dev, int id, int lun, bool verbose)
  	* block devices created
  	*/
  	snprintf(str, sizeof(str), "id%dlun%d", id, lun);
-	name = strdup(str);
-	if (!name)
-		return log_msg_ret("nam", -ENOMEM);
-	ret = blk_create_devicef(dev, "scsi_blk", name, UCLASS_SCSI, -1,
+	ret = blk_create_devicef(dev, "scsi_blk", str, UCLASS_SCSI, -1,

name is only used in a print statement and not passed on:

   snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
Quoted 3 lines
drivers/scsi/scsi.c +0 -0
  				 bd.blksz, bd.lba, &bdev);
  	if (ret) {
  		debug("Can't create device\n");
#2 lore raw
In reply to Bin Meng
Quoted 6 lines · Bin Meng

There is a memory leak during the scsi scan process due to the strdup'ed name string is never freed. Actually it is unnecessary to pass a strdup'ed name string to blk_create_devicef() as we can use the name string on the stack directly.

Applied to u-boot/master, thanks!

[1/1] scsi: Fix the name string memory leak during scsi scan

      commit: c6e8befd4b8da4a99c779a0fddd92690e2f611fa
Signature · 1 line
Tom