linux-xfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] xfsdump: Fix memory leak
@ 2023-12-14 12:17 Pavel Reichl
  2023-12-14 17:09 ` Darrick J. Wong
  0 siblings, 1 reply; 5+ messages in thread
From: Pavel Reichl @ 2023-12-14 12:17 UTC (permalink / raw)
  To: linux-xfs; +Cc: cem

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


^ permalink raw reply related	[flat|nested] 5+ messages in thread
* [PATCH] xfsdump: fix memory leak
@ 2022-09-22  3:02 zhanchengbin
  0 siblings, 0 replies; 5+ messages in thread
From: zhanchengbin @ 2022-09-22  3:02 UTC (permalink / raw)
  To: cem; +Cc: linfeilong, liuzhiqiang26, linux-xfs

Need free tmphdr and newnode before return,
otherwise it will cause memory leak.

Signed-off-by: zhanchengbin <zhanchengbin1@huawei.com>
---
  common/drive_simple.c | 1 +
  invutil/list.c        | 4 +++-
  2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/common/drive_simple.c b/common/drive_simple.c
index 5c3ed4b..141fdcb 100644
--- a/common/drive_simple.c
+++ b/common/drive_simple.c
@@ -456,6 +456,7 @@ do_begin_read(drive_t *drivep)
  	/* can only read one media file
  	 */
  	if (contextp->dc_fmarkcnt > 0) {
+		free(tmphdr);
  		return DRIVE_ERROR_EOM;
  	}

diff --git a/invutil/list.c b/invutil/list.c
index 46fb291..a3a4cfd 100644
--- a/invutil/list.c
+++ b/invutil/list.c
@@ -52,8 +52,10 @@ node_create(int hidden, int expanded, int level, int 
deleted, int file_idx, char
  	return NULL;

      newdata = malloc(sizeof(*newdata));
-    if(newdata == NULL)
+    if(newdata == NULL) {
+	free(newnode);
  	return NULL;
+    }

      newdata->hidden = hidden;
      newdata->expanded = expanded;
-- 
2.33.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread
* [PATCH] xfsdump: Fix memory leak
@ 2017-07-05 19:06 Paulo Alcantara
  2017-07-11  0:16 ` Eric Sandeen
  0 siblings, 1 reply; 5+ messages in thread
From: Paulo Alcantara @ 2017-07-05 19:06 UTC (permalink / raw)
  To: linux-xfs; +Cc: Paulo Alcantara

This patch fixes the following memory leak reported by valgrind:

==9198==
==9198== HEAP SUMMARY:
==9198==     in use at exit: 272,248 bytes in 13 blocks
==9198==   total heap usage: 629 allocs, 616 frees, 354,203 bytes
allocated
==9198==
==9198== 4,096 bytes in 1 blocks are definitely lost in loss record 12
of 13
==9198==    at 0x4C2FA50: calloc (vg_replace_malloc.c:711)
==9198==    by 0x415801: global_hdr_alloc (global.c:80)
==9198==    by 0x403D12: main (main.c:494)
==9198==
==9198== LEAK SUMMARY:
==9198==    definitely lost: 4,096 bytes in 1 blocks
==9198==    indirectly lost: 0 bytes in 0 blocks
==9198==      possibly lost: 0 bytes in 0 blocks
==9198==    still reachable: 268,152 bytes in 12 blocks
==9198==         suppressed: 0 bytes in 0 blocks
==9198== Reachable blocks (those to which a pointer was found) are not
shown.
==9198== To see them, rerun with: --leak-check=full
--show-leak-kinds=all
==9198==
==9198== For counts of detected and suppressed errors, rerun with: -v
==9198== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

Command was:
    # xfsdump -f /dev/vdb

Signed-off-by: Paulo Alcantara <pcacjr@gmail.com>
---
 common/main.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/common/main.c b/common/main.c
index 3848499..b34e974 100644
--- a/common/main.c
+++ b/common/main.c
@@ -163,6 +163,7 @@ main( int argc, char *argv[] )
 	bool_t ok;
 	/* REFERENCED */
 	int rval;
+	int err;
 
 	/* sanity checks
 	 */
@@ -564,7 +565,8 @@ main( int argc, char *argv[] )
 	ok = content_init( argc, argv, vmsz / VMSZ_PER );
 #endif /* RESTORE */
 	if ( ! ok ) {
-		return mlog_exit(EXIT_ERROR, RV_INIT);
+		err = mlog_exit(EXIT_ERROR, RV_INIT);
+		goto err_free;
 	}
 
 	/* if in a pipeline, go single-threaded with just one stream.
@@ -587,11 +589,13 @@ main( int argc, char *argv[] )
 				  ( global_hdr_t * )0 );
 #endif /* RESTORE */
 		if ( ! ok ) {
-			return mlog_exit(EXIT_ERROR, RV_INIT);
+			err = mlog_exit(EXIT_ERROR, RV_INIT);
+			goto err_free;
 		}
 		ok = drive_init3( );
 		if ( ! ok ) {
-			return mlog_exit(EXIT_ERROR, RV_INIT);
+			err = mlog_exit(EXIT_ERROR, RV_INIT);
+			goto err_free;
 		}
 #ifdef DUMP
 		exitcode = content_stream_dump( 0 );
@@ -602,12 +606,13 @@ main( int argc, char *argv[] )
 		if ( exitcode != EXIT_NORMAL ) {
 			( void )content_complete( );
 						/* for cleanup side-effect */
-			return mlog_exit(exitcode, RV_UNKNOWN);
+			err = mlog_exit(exitcode, RV_UNKNOWN);
 		} else if ( content_complete( )) {
-			return mlog_exit(EXIT_NORMAL, RV_OK);
+			err = mlog_exit(EXIT_NORMAL, RV_OK);
 		} else {
-			return mlog_exit(EXIT_INTERRUPT, RV_UNKNOWN);
+			err = mlog_exit(EXIT_INTERRUPT, RV_UNKNOWN);
 		}
+		goto err_free;
 	}
 
 	/* used to skip to end if errors occur during any
@@ -873,7 +878,14 @@ main( int argc, char *argv[] )
 				mlog_exit_hint(RV_INCOMPLETE);
 		}
 	}
-	return mlog_exit(exitcode, RV_UNKNOWN);
+
+	err = mlog_exit(exitcode, RV_UNKNOWN);
+
+err_free:
+#ifdef DUMP
+	global_hdr_free( gwhdrtemplatep );
+#endif /* DUMP */
+	return err;
 }
 
 #define ULO( f, o )	fprintf( stderr,		\
-- 
2.9.4


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-12-14 17:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-14 12:17 [PATCH] xfsdump: Fix memory leak Pavel Reichl
2023-12-14 17:09 ` 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).