Skip to contentSkip to conversation

nbd: fix possible memory leak

PATCH Superseded Sudip Mukherjee 2 participants 2 messages Jan 13, 2015 – Jan 26, 2015

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nbd: fix possible memory leak

we have already allocated memory for nbd_dev, but we were not releasing that memory and just returning the error value.

---

Hi Paul,
after seeing your mail about maintainers, just started to see the code. this patch is one way, another way can be to bring the kcalloc part below these checks.

 drivers/block/nbd.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/block/nbd.c +9 -4
index 4bc2a5c..13ea239 100644
@@ -822,11 +822,15 @@ static int __init nbd_init(void)
 		max_part = (1UL << part_shift) - 1;
 	}
 
-	if ((1UL << part_shift) > DISK_MAX_PARTS)
-		return -EINVAL;
+	if ((1UL << part_shift) > DISK_MAX_PARTS) {
+		err = -EINVAL;
+		goto out_free;
+	}
 
-	if (nbds_max > 1UL << (MINORBITS - part_shift))
-		return -EINVAL;
+	if (nbds_max > 1UL << (MINORBITS - part_shift)) {
+		err = -EINVAL;
+		goto out_free;
+	}
 
 	for (i = 0; i < nbds_max; i++) {
 		struct gendisk *disk = alloc_disk(1 << part_shift);
@@ -889,6 +893,7 @@ out:
 		blk_cleanup_queue(nbd_dev[i].disk->queue);
 		put_disk(nbd_dev[i].disk);
 	}
+out_free:
 	kfree(nbd_dev);
 	return err;
 }
Signature · 1 line
1.8.1.2
#1 lore raw

Sudip,

Quoted 9 lines · Sudip Mukherjee

we have already allocated memory for nbd_dev, but we were not releasing that memory and just returning the error value.

---

Hi Paul,
after seeing your mail about maintainers, just started to see the code. this patch is one way, another way can be to bring the kcalloc part below these checks.

Probably better to move the kcalloc below, rather than adding more branches, wouldn't you agree?

Thanks,
Paul