Skip to contentSkip to conversation

xfsdump: Fix memory leak

PATCH Superseded Pavel Reichl 2 participants 2 messages Dec 14, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfsdump: Fix memory leak

Fix memory leak found by coverity.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1554295: Resource leaks (RESOURCE_LEAK)
Failing to save or free storage allocated by strdup(path) leaks it.

---
 restore/tree.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
restore/tree.c +14 -1
index 6f3180f..66dd9df 100644
@@ -4977,9 +4977,22 @@ static int
 mkdir_r(char *path)
 {
 	struct stat sbuf;
+	char *path_copy;
+	int ret;
 
 	if (stat(path, &sbuf) < 0) {
-		if (mkdir_r(dirname(strdup(path))) < 0)
+		path_copy = strdup(path);
+		if (!path_copy) {
+			mlog(MLOG_TRACE | MLOG_ERROR | MLOG_TREE, _(
+				"unable to allocate memory for a path\n"));
+			mlog_exit(EXIT_ERROR, RV_ERROR);
+			exit(1);
+		}
+
+		ret = mkdir_r(dirname(path_copy));
+		free(path_copy);
+
+		if (ret < 0)
 			return -1;
 		return mkdir(path, 0755);
 	}
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 17 lines · Pavel Reichl

Fix memory leak found by coverity.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1554295: Resource leaks (RESOURCE_LEAK)
Failing to save or free storage allocated by strdup(path) leaks it.

---
 restore/tree.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
restore/tree.c +0 -0
index 6f3180f..66dd9df 100644
@@ -4977,9 +4977,22 @@ static int
 mkdir_r(char *path)
 {

restore/tree.c static int · line 4987

Quoted 10 lines
restore/tree.c +6 -1
 	struct stat sbuf;
+	char *path_copy;
+	int ret;
 
 	if (stat(path, &sbuf) < 0) {
-		if (mkdir_r(dirname(strdup(path))) < 0)
+		path_copy = strdup(path);
+		if (!path_copy) {
+			mlog(MLOG_TRACE | MLOG_ERROR | MLOG_TREE, _(
+				"unable to allocate memory for a path\n"));

Nit: the _( should be on the same line as the format string, right?

With that fixed,

--D

Quoted 15 lines
restore/tree.c +8 -0
+			mlog_exit(EXIT_ERROR, RV_ERROR);
+			exit(1);
+		}
+
+		ret = mkdir_r(dirname(path_copy));
+		free(path_copy);
+
+		if (ret < 0)
 			return -1;
 		return mkdir(path, 0755);
 	}
Signature · 1 line
2.43.0