Skip to contentSkip to conversation

Fix memory leak for function Manage_subdevs Manage_add Kill

PATCH New miaoguanqin 1 participant 1 message Feb 10, 2023 MD RAID

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix memory leak for function Manage_subdevs Manage_add Kill

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.

---
 Kill.c   | 12 ++++++++++--
 Manage.c |  9 ++++++++-
 2 files changed, 18 insertions(+), 3 deletions(-)
Kill.c +10 -2
index d4767e2..d1207cf 100644
@@ -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;
 }
Manage.c +8 -1
index ffe55f8..15635eb 100644
@@ -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)
Signature · 1 line
2.33.0