diff mbox series

scsi: ufs: ufshcd-pltfrm: check the return value of kstrdup()

Message ID tencent_D3914A6FFF832049CC70B9411CAD1492E108@qq.com
State New
Headers show
Series scsi: ufs: ufshcd-pltfrm: check the return value of kstrdup() | expand

Commit Message

Xiaoke Wang Dec. 11, 2021, 6:14 p.m. UTC
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 <xkernel.wang@foxmail.com>
---
 drivers/scsi/ufs/ufshcd-pltfrm.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

--

Comments

Bart Van Assche Dec. 12, 2021, 10:32 p.m. UTC | #1
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 <xkernel.wang@foxmail.com>

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.
diff mbox series

Patch

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)) {