diff mbox series

Fix memory leak for function Manage_subdevs Manage_add Kill

Message ID ef9c2ae2-2102-c1ad-5113-39809afe5934@huawei.com (mailing list archive)
State Superseded, archived
Headers show
Series Fix memory leak for function Manage_subdevs Manage_add Kill | expand

Commit Message

Guanqin Miao Feb. 10, 2023, 10:28 a.m. UTC
When we excute mdadm,we found some memory leak.
The function call stack is as follows:
#0 in xcalloc
#1 in guess_super_type
#2 in guess_super
#3 in Kill
#4 in misc_list
#5 in main

#0 in __interceptor_posix_memalign
#1 in init_super1
#2 in Kill
#3 in misc_list
#4 in main

#0 in __interceptor_calloc
#1 in xcalloc
#2 in match_metadata_desc1
#3 in super_by_fd
#4 in Manage_subdevs
#5 in main

#0 in __interceptor_calloc
#1 in xcalloc
#2 in dup_super
#3 in Manage_add
#4 in Manage_subdevs
#5 in main

We fix these memory leak based on code logic.

Signed-off-by: miaoguanqin <miaoguanqin@huawei.com>
---
  Kill.c   | 12 ++++++++++--
  Manage.c |  9 ++++++++-
  2 files changed, 18 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/Kill.c b/Kill.c
index d4767e2..d1207cf 100644
--- a/Kill.c
+++ b/Kill.c
@@ -40,7 +40,8 @@  int Kill(char *dev, struct supertype *st, int force, 
int verbose, int noexcl)
  	 *  2 - failed to open the device.
  	 *  4 - failed to find a superblock.
  	 */
-
+	
+	int flags = 0;
  	int fd, rv = 0;

  	if (force)
@@ -52,8 +53,10 @@  int Kill(char *dev, struct supertype *st, int force, 
int verbose, int noexcl)
  				dev);
  		return 2;
  	}
-	if (st == NULL)
+	if (st == NULL) {
  		st = guess_super(fd);
+		flags = 1;
+	}
  	if (st == NULL || st->ss->init_super == NULL) {
  		if (verbose >= 0)
  			pr_err("Unrecognised md component device - %s\n", dev);
@@ -77,6 +80,11 @@  int Kill(char *dev, struct supertype *st, int force, 
int verbose, int noexcl)
  			rv = 0;
  		}
  	}
+	if (flags == 1 && st) {
+		if (st->sb)
+			free(st->sb);
+		free(st);
+	}
  	close(fd);
  	return rv;
  }
diff --git a/Manage.c b/Manage.c
index ffe55f8..15635eb 100644
--- a/Manage.c
+++ b/Manage.c
@@ -819,9 +819,14 @@  int Manage_add(int fd, int tfd, struct mddev_dev *dv,
  						    rdev, update, devname,
  						    verbose, array);
  				dev_st->ss->free_super(dev_st);
-				if (rv)
+				if (rv){
+					if (dev_st)
+						free(dev_st);
  					return rv;
+				}
  			}
+			if (dev_st)
+				free(dev_st);
  		}
  		if (dv->disposition == 'M') {
  			if (verbose > 0)
@@ -1649,6 +1654,8 @@  int Manage_subdevs(char *devname, int fd,
  			break;
  		}
  	}
+	if (tst)
+		free(tst);
  	if (frozen > 0)
  		sysfs_set_str(&info, NULL, "sync_action","idle");
  	if (test && count == 0)