autofs-5.1.9 - fix possible memory leak in mnts_get_expire_list()

From: Ian Kent <raven@themaw.net>

If there's a memory allocation failure when building the tree of mounts
for expire the mount in the process of being added will hold a reference
but will not be added to the expire list so it will not be dropped when
the list is freed.

Taking the reference when the list entry is being added is safe because
this operation won't fail.

Signed-off-by: Ian Kent <raven@themaw.net>
---
 CHANGELOG    |    1 +
 lib/mounts.c |    6 +++---
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 2168a004b..30c623419 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -36,6 +36,7 @@
 - fix invalidated map entry handling in hosts module.
 - dont call mkdir if dir exists.
 - check map source in master_parse_entry().
+- fix possible memory leak in mnts_get_expire_list().
 
 02/11/2023 autofs-5.1.9
 - fix kernel mount status notification.
diff --git a/lib/mounts.c b/lib/mounts.c
index ab16252c7..6f9cc3109 100644
--- a/lib/mounts.c
+++ b/lib/mounts.c
@@ -1481,8 +1481,10 @@ static int tree_mnt_expire_list_work(struct tree_node *n, void *ptr)
 	 * as expiring the offset tree root itself (if theree is a root
 	 * offset).
 	 */
-	if (mnt->mp[mnt->len - 1] != '/')
+	if (mnt->mp[mnt->len - 1] != '/') {
+		__mnts_get_mount(mnt);
 		list_add(&mnt->expire, mnts);
+	}
 
 	return 1;
 }
@@ -1502,8 +1504,6 @@ void mnts_get_expire_list(struct list_head *mnts, struct autofs_point *ap)
 		if (!(mnt->flags & MNTS_MOUNTED))
 			continue;
 
-		__mnts_get_mount(mnt);
-
 		if (!tree) {
 			tree = tree_mnt_root(mnt);
 			if (!tree) {
