All of lore.kernel.org
 help / color / mirror / Atom feed
From: Pavel Reichl <preichl@redhat.com>
To: linux-xfs@vger.kernel.org
Cc: cem@kernel.org
Subject: [PATCH] xfsdump: Fix memory leak
Date: Thu, 14 Dec 2023 13:17:15 +0100	[thread overview]
Message-ID: <20231214121715.562273-1-preichl@redhat.com> (raw)

Fix memory leak found by coverity.

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

Signed-off-by: Pavel Reichl <preichl@redhat.com>
---
 restore/tree.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/restore/tree.c b/restore/tree.c
index 6f3180f..66dd9df 100644
--- a/restore/tree.c
+++ b/restore/tree.c
@@ -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);
 	}
-- 
2.43.0


             reply	other threads:[~2023-12-14 12:17 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-14 12:17 Pavel Reichl [this message]
2023-12-14 17:09 ` [PATCH] xfsdump: Fix memory leak Darrick J. Wong
  -- strict thread matches above, loose matches on Subject: below --
2022-09-22  3:02 [PATCH] xfsdump: fix " zhanchengbin
2017-07-05 19:06 [PATCH] xfsdump: Fix " Paulo Alcantara
2017-07-11  0:16 ` Eric Sandeen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231214121715.562273-1-preichl@redhat.com \
    --to=preichl@redhat.com \
    --cc=cem@kernel.org \
    --cc=linux-xfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.