Blame SOURCES/autofs-5.1.7-refactor-lookup_prune_one_cache-a-bit.patch

96dc52
autofs-5.1.7 - refactor lookup_prune_one_cache() a bit
96dc52
96dc52
From: Ian Kent <raven@themaw.net>
96dc52
96dc52
Coverity: use: Using an unreliable value of "me" inside the second locked
96dc52
	  section.
96dc52
96dc52
Change lookup_prune_one_cache() a little, move the location the next
96dc52
key is set (before releasing the lock) and add a comment explaining
96dc52
why we don't care about the side effects of the read lock release/
96dc52
write lock aquire/write lock release/read lock reaquire.
96dc52
96dc52
Signed-off-by: Ian Kent <raven@themaw.net>
96dc52
---
96dc52
 CHANGELOG       |    1 +
96dc52
 daemon/lookup.c |   20 +++++++++++++++++++-
96dc52
 2 files changed, 20 insertions(+), 1 deletion(-)
96dc52
96dc52
diff --git a/CHANGELOG b/CHANGELOG
96dc52
index 81461978..b79aebc8 100644
96dc52
--- a/CHANGELOG
96dc52
+++ b/CHANGELOG
96dc52
@@ -63,6 +63,7 @@
96dc52
 - fix arg not used in error print.
96dc52
 - fix missing lock release in mount_subtree().
96dc52
 - fix double free in parse_mapent().
96dc52
+- refactor lookup_prune_one_cache() a bit.
96dc52
 
96dc52
 25/01/2021 autofs-5.1.7
96dc52
 - make bind mounts propagation slave by default.
96dc52
diff --git a/daemon/lookup.c b/daemon/lookup.c
96dc52
index 32dbc24d..3e9722e4 100644
96dc52
--- a/daemon/lookup.c
96dc52
+++ b/daemon/lookup.c
96dc52
@@ -1375,7 +1375,6 @@ void lookup_prune_one_cache(struct autofs_point *ap, struct mapent_cache *mc, ti
96dc52
 		}
96dc52
 
96dc52
 		key = strdup(me->key);
96dc52
-		me = cache_enumerate(mc, me);
96dc52
 		/* Don't consider any entries with a wildcard */
96dc52
 		if (!key || strchr(key, '*')) {
96dc52
 			if (key)
96dc52
@@ -1422,6 +1421,7 @@ void lookup_prune_one_cache(struct autofs_point *ap, struct mapent_cache *mc, ti
96dc52
 		if (valid)
96dc52
 			cache_unlock(valid->mc);
96dc52
 
96dc52
+		me = cache_enumerate(mc, me);
96dc52
 		if (me)
96dc52
 			next_key = strdup(me->key);
96dc52
 
96dc52
@@ -1456,6 +1456,24 @@ void lookup_prune_one_cache(struct autofs_point *ap, struct mapent_cache *mc, ti
96dc52
 next:
96dc52
 		cache_readlock(mc);
96dc52
 		if (next_key) {
96dc52
+			/* The lock release and reaquire above can mean
96dc52
+			 * a number of things could happen.
96dc52
+			 *
96dc52
+			 * First, mapents could be added between the
96dc52
+			 * current mapent and the mapent of next_key.
96dc52
+			 * Don't care about that because there's no
96dc52
+			 * need to prune newly added entries.
96dc52
+			 *
96dc52
+			 * Second, the next mapent data could have
96dc52
+			 * changed. Don't care about that either since
96dc52
+			 * we are looking to prune stale map entries
96dc52
+			 * and don't care when they become stale.
96dc52
+			 *
96dc52
+			 * Finally, the mapent of next_key could have
96dc52
+			 * gone away. Again don't care about this either,
96dc52
+			 * the loop will exit prematurely so just wait
96dc52
+			 * until the next prune and try again.
96dc52
+			 */
96dc52
 			me = cache_lookup_distinct(mc, next_key);
96dc52
 			free(next_key);
96dc52
 		}