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

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