osmo-bsc: Only keep the newest of subsequent invalid positions
diff --git a/openbsc/src/osmo-bsc/osmo_bsc_main.c b/openbsc/src/osmo-bsc/osmo_bsc_main.c
index 69622bb..7d376ef 100644
--- a/openbsc/src/osmo-bsc/osmo_bsc_main.c
+++ b/openbsc/src/osmo-bsc/osmo_bsc_main.c
@@ -185,7 +185,7 @@
 void cleanup_locations()
 {
 	struct location *myloc, *tmp;
-	int i = 0;
+	int invalpos = 0, i = 0;
 
 	LOGP(DCTRL, LOGL_DEBUG, "Checking position list.\n");
 	llist_for_each_entry_safe(myloc, tmp, &locations, list) {
@@ -194,6 +194,17 @@
 			LOGP(DCTRL, LOGL_DEBUG, "Deleting old position.\n");
 			llist_del(&myloc->list);
 			talloc_free(myloc);
+		} else if (!myloc->valid) { /* Only capture the newest of subsequent invalid positions */
+			invalpos++;
+			if (invalpos > 1) {
+				LOGP(DCTRL, LOGL_DEBUG, "Deleting subsequent invalid position.\n");
+				invalpos--;
+				i--;
+				llist_del(&myloc->list);
+				talloc_free(myloc);
+			}
+		} else {
+			invalpos = 0;
 		}
 	}
 	LOGP(DCTRL, LOGL_DEBUG, "Found %i positions.\n", i);