[PATCH 2/5] cdev-alias: fix memory leak in diskuuid handling

Ahmad Fatoum a.fatoum at pengutronix.de
Thu Dec 11 13:00:44 PST 2025


uuid was duplicated but not free'd in error cases. Fix that.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 common/cdev-alias.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/common/cdev-alias.c b/common/cdev-alias.c
index 1878553615d2..3732fd90525f 100644
--- a/common/cdev-alias.c
+++ b/common/cdev-alias.c
@@ -43,12 +43,15 @@ static int cdev_alias_resolve_diskuuid(struct cdev_alias_res *cdev_alias_res,
 				       void *data)
 {
 	struct cdev *cdev;
-	char *arg;
+	char *str, *arg;
+	int ret = 0;
 
-	arg = xstrdup(uuid);
+	arg = str = xstrdup(uuid);
 	uuid = strsep(&arg, ".");
-	if (!uuid || !*uuid)
-		return -EINVAL;
+	if (!uuid || !*uuid) {
+		ret = -EINVAL;
+		goto out;
+	}
 
 	for_each_cdev(cdev) {
 		if (cdev_is_partition(cdev))
@@ -59,14 +62,19 @@ static int cdev_alias_resolve_diskuuid(struct cdev_alias_res *cdev_alias_res,
 
 		if (arg) {
 			cdev = cdev_find_partition(cdev, arg);
-			if (!cdev)
-				return -ENODEV;
+			if (!cdev) {
+				ret = -ENODEV;
+				break;
+			}
 		}
 
-		return fn(cdev, data);
+		ret = fn(cdev, data);
+		break;
 	}
 
-	return 0;
+out:
+	free(str);
+	return ret;
 }
 
 static struct cdev_alias_res cdev_alias_aliases[] = {
-- 
2.47.3




More information about the barebox mailing list