soc: realview: fix two memleaks in realview_soc_probe

Message ID 20231207121932.3353687-1-alexious@zju.edu.cn
State New
Headers
Series soc: realview: fix two memleaks in realview_soc_probe |

Commit Message

Zhipeng Lu Dec. 7, 2023, 12:19 p.m. UTC
  When of_property_read_string fails, it should free soc_dev_attr
allocated by kzalloc before. So as the failure of regmap_read, the
soc_dev needs to be unregistered and soc_dev_attr needs to be freed.

Fixes: a2974c9c1f83 ("soc: add driver for the ARM RealView")
Signed-off-by: Zhipeng Lu <alexious@zju.edu.cn>
---
 drivers/soc/versatile/soc-realview.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
  

Patch

diff --git a/drivers/soc/versatile/soc-realview.c b/drivers/soc/versatile/soc-realview.c
index c6876d232d8f..19c4a9599c9f 100644
--- a/drivers/soc/versatile/soc-realview.c
+++ b/drivers/soc/versatile/soc-realview.c
@@ -99,8 +99,10 @@  static int realview_soc_probe(struct platform_device *pdev)
 
 	ret = of_property_read_string(np, "compatible",
 				      &soc_dev_attr->soc_id);
-	if (ret)
+	if (ret) {
+		kfree(soc_dev_attr);
 		return -EINVAL;
+	}
 
 	soc_dev_attr->machine = "RealView";
 	soc_dev_attr->family = "Versatile";
@@ -112,9 +114,11 @@  static int realview_soc_probe(struct platform_device *pdev)
 	}
 	ret = regmap_read(syscon_regmap, REALVIEW_SYS_ID_OFFSET,
 			  &realview_coreid);
-	if (ret)
+	if (ret) {
+		soc_device_unregister(soc_dev);
+		kfree(soc_dev_attr);
 		return -ENODEV;
-
+	}
 	dev_info(&pdev->dev, "RealView Syscon Core ID: 0x%08x, HBI-%03x\n",
 		 realview_coreid,
 		 ((realview_coreid >> 16) & 0xfff));