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

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