Skip to contentSkip to conversation

libf2fs: fix memory leak caused by get_rootdev()

PATCH New Junyong Sun 1 participant 1 message Mar 2, 2021 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libf2fs: fix memory leak caused by get_rootdev()

memory which malloc for uevent don't free at get_rootdev(), it would cause memory leak, so free it before return.

---
 lib/libf2fs.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
lib/libf2fs.c +8 -3
index 55fa391..85107ee 100644
@@ -637,20 +637,25 @@ char *get_rootdev()
 
 	ptr = strstr(uevent, "DEVNAME");
 	if (!ptr)
-		return NULL;
+		goto out_free;
 
 	ret = sscanf(ptr, "DEVNAME=%s\n", buf);
 	if (strlen(buf) == 0)
-		return NULL;
+		goto out_free;
 
 	ret = strlen(buf) + 5;
 	rootdev = malloc(ret + 1);
 	if (!rootdev)
-		return NULL;
+		goto out_free;
 	rootdev[ret] = '\0';
 
 	snprintf(rootdev, ret + 1, "/dev/%s", buf);
+	free(uevent);
 	return rootdev;
+
+out_free:
+	free(uevent);
+	return NULL;
 #endif
 }
 
Signature · 8 lines
2.7.4



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel