Blame SOURCES/0056-Monitor-Fix-statelist-memory-leaks.patch

37f2b0
From 55c10e4de13abe3e6934895e1fff7d2d20d0b2c2 Mon Sep 17 00:00:00 2001
37f2b0
From: Pawel Baldysiak <pawel.baldysiak@intel.com>
37f2b0
Date: Thu, 1 Sep 2022 11:20:31 +0200
37f2b0
Subject: [PATCH 56/83] Monitor: Fix statelist memory leaks
37f2b0
37f2b0
Free statelist in error path in Monitor initialization.
37f2b0
37f2b0
Signed-off-by: Pawel Baldysiak <pawel.baldysiak@intel.com>
37f2b0
Signed-off-by: Jes Sorensen <jsorensen@fb.com>
37f2b0
---
37f2b0
 Monitor.c | 40 +++++++++++++++++++++++++++++++---------
37f2b0
 1 file changed, 31 insertions(+), 9 deletions(-)
37f2b0
37f2b0
diff --git a/Monitor.c b/Monitor.c
37f2b0
index 93f36ac0..b4e954c6 100644
37f2b0
--- a/Monitor.c
37f2b0
+++ b/Monitor.c
37f2b0
@@ -74,6 +74,7 @@ static int add_new_arrays(struct mdstat_ent *mdstat, struct state **statelist,
37f2b0
 			  int test, struct alert_info *info);
37f2b0
 static void try_spare_migration(struct state *statelist, struct alert_info *info);
37f2b0
 static void link_containers_with_subarrays(struct state *list);
37f2b0
+static void free_statelist(struct state *statelist);
37f2b0
 #ifndef NO_LIBUDEV
37f2b0
 static int check_udev_activity(void);
37f2b0
 #endif
37f2b0
@@ -128,7 +129,6 @@ int Monitor(struct mddev_dev *devlist,
37f2b0
 	 */
37f2b0
 
37f2b0
 	struct state *statelist = NULL;
37f2b0
-	struct state *st2;
37f2b0
 	int finished = 0;
37f2b0
 	struct mdstat_ent *mdstat = NULL;
37f2b0
 	char *mailfrom;
37f2b0
@@ -185,12 +185,14 @@ int Monitor(struct mddev_dev *devlist,
37f2b0
 				continue;
37f2b0
 			if (strcasecmp(mdlist->devname, "<ignore>") == 0)
37f2b0
 				continue;
37f2b0
+			if (!is_mddev(mdlist->devname)) {
37f2b0
+				free_statelist(statelist);
37f2b0
+				return 1;
37f2b0
+			}
37f2b0
 
37f2b0
 			st = xcalloc(1, sizeof *st);
37f2b0
 			snprintf(st->devname, MD_NAME_MAX + sizeof("/dev/md/"),
37f2b0
 				 "/dev/md/%s", basename(mdlist->devname));
37f2b0
-			if (!is_mddev(mdlist->devname))
37f2b0
-				return 1;
37f2b0
 			st->next = statelist;
37f2b0
 			st->devnm[0] = 0;
37f2b0
 			st->percent = RESYNC_UNKNOWN;
37f2b0
@@ -206,8 +208,10 @@ int Monitor(struct mddev_dev *devlist,
37f2b0
 		for (dv = devlist; dv; dv = dv->next) {
37f2b0
 			struct state *st;
37f2b0
 
37f2b0
-			if (!is_mddev(dv->devname))
37f2b0
+			if (!is_mddev(dv->devname)) {
37f2b0
+				free_statelist(statelist);
37f2b0
 				return 1;
37f2b0
+			}
37f2b0
 
37f2b0
 			st = xcalloc(1, sizeof *st);
37f2b0
 			mdlist = conf_get_ident(dv->devname);
37f2b0
@@ -294,16 +298,16 @@ int Monitor(struct mddev_dev *devlist,
37f2b0
 		for (stp = &statelist; (st = *stp) != NULL; ) {
37f2b0
 			if (st->from_auto && st->err > 5) {
37f2b0
 				*stp = st->next;
37f2b0
-				free(st->spare_group);
37f2b0
+				if (st->spare_group)
37f2b0
+					free(st->spare_group);
37f2b0
+
37f2b0
 				free(st);
37f2b0
 			} else
37f2b0
 				stp = &st->next;
37f2b0
 		}
37f2b0
 	}
37f2b0
-	for (st2 = statelist; st2; st2 = statelist) {
37f2b0
-		statelist = st2->next;
37f2b0
-		free(st2);
37f2b0
-	}
37f2b0
+
37f2b0
+	free_statelist(statelist);
37f2b0
 
37f2b0
 	if (pidfile)
37f2b0
 		unlink(pidfile);
37f2b0
@@ -1056,6 +1060,24 @@ static void link_containers_with_subarrays(struct state *list)
37f2b0
 				}
37f2b0
 }
37f2b0
 
37f2b0
+/**
37f2b0
+ * free_statelist() - Frees statelist.
37f2b0
+ * @statelist: statelist to free
37f2b0
+ */
37f2b0
+static void free_statelist(struct state *statelist)
37f2b0
+{
37f2b0
+	struct state *tmp = NULL;
37f2b0
+
37f2b0
+	while (statelist) {
37f2b0
+		if (statelist->spare_group)
37f2b0
+			free(statelist->spare_group);
37f2b0
+
37f2b0
+		tmp = statelist;
37f2b0
+		statelist = statelist->next;
37f2b0
+		free(tmp);
37f2b0
+	}
37f2b0
+}
37f2b0
+
37f2b0
 #ifndef NO_LIBUDEV
37f2b0
 /* function: check_udev_activity
37f2b0
  * Description: Function waits for udev to finish
37f2b0
-- 
37f2b0
2.38.1
37f2b0