2021-12-11 18:23:07

by Xiaoke Wang

[permalink] [raw]
Subject: [PATCH] scsi: ufs: ufshcd-pltfrm: check the return value of kstrdup()

kstrdup() can return NULL if some internal memory errors happen, so it
is better to check the return value of it.

Signed-off-by: xkernel <[email protected]>
---
drivers/scsi/ufs/ufshcd-pltfrm.c | 12 +++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/drivers/scsi/ufs/ufshcd-pltfrm.c b/drivers/scsi/ufs/ufshcd-pltfrm.c
index 8859c13..32e7bd3 100644
--- a/drivers/scsi/ufs/ufshcd-pltfrm.c
+++ b/drivers/scsi/ufs/ufshcd-pltfrm.c
@@ -89,9 +89,15 @@ static int ufshcd_parse_clock_info(struct ufs_hba *hba)
goto out;
}

+ clki->name = kstrdup(name, GFP_KERNEL);
+ if (!clki->name) {
+ ret = -ENOMEM;
+ devm_kfree(dev, clki);
+ goto out;
+ }
+
clki->min_freq = clkfreq[i];
clki->max_freq = clkfreq[i+1];
- clki->name = kstrdup(name, GFP_KERNEL);
if (!strcmp(name, "ref_clk"))
clki->keep_link_active = true;
dev_dbg(dev, "%s: min %u max %u name %s\n", "freq-table-hz",
@@ -127,6 +133,10 @@ static int ufshcd_populate_vreg(struct device *dev, const char *name,
return -ENOMEM;

vreg->name = kstrdup(name, GFP_KERNEL);
+ if (!vreg->name) {
+ devm_kfree(dev, vreg);
+ return -ENOMEM;
+ }

snprintf(prop_name, MAX_PROP_SIZE, "%s-max-microamp", name);
if (of_property_read_u32(np, prop_name, &vreg->max_uA)) {
--


2021-12-12 22:32:39

by Bart Van Assche

[permalink] [raw]
Subject: Re: [PATCH] scsi: ufs: ufshcd-pltfrm: check the return value of kstrdup()

On 12/11/21 10:14, xkernel wrote:
> kstrdup() can return NULL if some internal memory errors happen, so it
> is better to check the return value of it.
>
> Signed-off-by: xkernel <[email protected]>

Is xkernel the name of a person or the name of a robot? Patches should
be signed by a person even if these have been generated by a robot.

> + clki->name = kstrdup(name, GFP_KERNEL);
> + if (!clki->name) {
> + ret = -ENOMEM;
> + devm_kfree(dev, clki);
> + goto out;
> + }

Is the devm_kfree() call necessary? Is it useful?

Thanks,

Bart.

2021-12-13 01:58:02

by Xiaoke Wang

[permalink] [raw]
Subject: Re: [PATCH] scsi: ufs: ufshcd-pltfrm: check the return value of kstrdup()

On 12/12/21 22:32, Bart Van Assche wrote:
> Is xkernel the name of a person or the name of a robot? Patches should
> be signed by a person even if these have been generated by a robot.
>
> > + clki->name = kstrdup(name, GFP_KERNEL);
> > + if (!clki->name) {
> > + ret = -ENOMEM;
> > + devm_kfree(dev, clki);
> > + goto out;
> > + }
>
> Is the devm_kfree() call necessary? Is it useful?

Note: the last mail seems fail because of the html content.

Sorry about that, xkernel is my English name I named myself, my full
name in Chinese format is "Xiaoke Wang". Thank you for your
suggestion, I will consider using my official name in the future.

clki is allocated on above of kstrdup(), line 86-90, and if clki is NULL
will return -ENOMEM which is the same return value with the patch. So
I think clki should be freed to prevent potential memory leak:
> clki = devm_kzalloc(dev, sizeof(*clki), GFP_KERNEL);
> if (!clki) {
> ret = -ENOMEM;
> goto out;
> }

vreg is in similar case.