diff mbox series

[-next] SCSI: fix possible memory leak while device_add() fails

Message ID 20230801095357.44778-1-wangzhu9@huawei.com (mailing list archive)
State Superseded
Headers show
Series [-next] SCSI: fix possible memory leak while device_add() fails | expand

Commit Message

wangzhu Aug. 1, 2023, 9:53 a.m. UTC
If device_add() returns error, the name allocated by dev_set_name() need
be freed. As comment of device_add() says, it should use put_device() to
decrease the reference count in the error path. So fix this by calling
put_device, then the name can be freed in kobject_cleanp().

Fixes: ee959b00c335 ("SCSI: convert struct class_device to struct device")
Signed-off-by: Zhu Wang <wangzhu9@huawei.com>
---
 drivers/scsi/raid_class.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

Bart Van Assche Aug. 1, 2023, 6:24 p.m. UTC | #1
On 8/1/23 02:53, Zhu Wang wrote:
> If device_add() returns error, the name allocated by dev_set_name() need
> be freed. As comment of device_add() says, it should use put_device() to
> decrease the reference count in the error path. So fix this by calling
> put_device, then the name can be freed in kobject_cleanp().
> 
> Fixes: ee959b00c335 ("SCSI: convert struct class_device to struct device")
> Signed-off-by: Zhu Wang <wangzhu9@huawei.com>
> ---
>   drivers/scsi/raid_class.c | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/scsi/raid_class.c b/drivers/scsi/raid_class.c
> index 898a0bdf8df6..2ba4da8d822d 100644
> --- a/drivers/scsi/raid_class.c
> +++ b/drivers/scsi/raid_class.c
> @@ -242,8 +242,10 @@ int raid_component_add(struct raid_template *r,struct device *raid_dev,
>   	list_add_tail(&rc->node, &rd->component_list);
>   	rc->dev.class = &raid_class.class;
>   	err = device_add(&rc->dev);
> -	if (err)
> +	if (err) {
> +		put_device(&rc->dev);
>   		goto err_out;
> +	}
>   
>   	return 0;

Please move the new put_device() call under the "err_out:" label.

Thanks,

Bart.
diff mbox series

Patch

diff --git a/drivers/scsi/raid_class.c b/drivers/scsi/raid_class.c
index 898a0bdf8df6..2ba4da8d822d 100644
--- a/drivers/scsi/raid_class.c
+++ b/drivers/scsi/raid_class.c
@@ -242,8 +242,10 @@  int raid_component_add(struct raid_template *r,struct device *raid_dev,
 	list_add_tail(&rc->node, &rd->component_list);
 	rc->dev.class = &raid_class.class;
 	err = device_add(&rc->dev);
-	if (err)
+	if (err) {
+		put_device(&rc->dev);
 		goto err_out;
+	}
 
 	return 0;