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

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