From a326ed70228fd1dff16d6f386bbba2760897f627 Mon Sep 17 00:00:00 2001
From: David Teigland <teigland@redhat.com>
Date: Tue, 25 Apr 2023 14:46:36 -0500
Subject: [PATCH] lvmcache: fix valgrind error when dropping md duplicate

When lvmcache info is dropped because it's an md component,
then the lvmcache vginfo can also be dropped, but the list
iterator was still using the list head in vginfo, so break
from the loop earlier to avoid it.
---
 lib/cache/lvmcache.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/lib/cache/lvmcache.c b/lib/cache/lvmcache.c
index bee63eb..f67eba2 100644
--- a/lib/cache/lvmcache.c
+++ b/lib/cache/lvmcache.c
@@ -1193,6 +1193,9 @@ void lvmcache_extra_md_component_checks(struct cmd_context *cmd)
 	 */
 
 	dm_list_iterate_items_safe(vginfo, vginfo2, &_vginfos) {
+		char vgid[ID_LEN + 1] __attribute__((aligned(8))) = { 0 };
+		memcpy(vgid, vginfo->vgid, ID_LEN);
+
 		dm_list_iterate_items_safe(info, info2, &vginfo->infos) {
 			dev = info->dev;
 			device_hint = _get_pvsummary_device_hint(dev->pvid);
@@ -1246,6 +1249,10 @@ void lvmcache_extra_md_component_checks(struct cmd_context *cmd)
 				lvmcache_del(info);
 				lvmcache_del_dev_from_duplicates(dev);
 				cmd->filter->wipe(cmd, cmd->filter, dev, NULL);
+
+				/* If vginfo was deleted don't continue using vginfo->infos */
+				if (!_search_vginfos_list(NULL, vgid))
+					break;
 			}
 		}
 	}
-- 
2.33.0

