From 24039928a5e987695a65ecbb504d4304c17bb616 Mon Sep 17 00:00:00 2001 From: pk Date: Sun, 17 Feb 2002 23:49:01 +0000 Subject: [PATCH] Prevent a premature end of the iteration if the first element in the list is removed from the callback function. --- usr.bin/make/lst.lib/lstForEachFrom.c | 30 +++++++++++++++++---------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/usr.bin/make/lst.lib/lstForEachFrom.c b/usr.bin/make/lst.lib/lstForEachFrom.c index 18f536a150d0..ea30a8b768bc 100644 --- a/usr.bin/make/lst.lib/lstForEachFrom.c +++ b/usr.bin/make/lst.lib/lstForEachFrom.c @@ -1,4 +1,4 @@ -/* $NetBSD: lstForEachFrom.c,v 1.7 1997/09/28 03:31:26 lukem Exp $ */ +/* $NetBSD: lstForEachFrom.c,v 1.8 2002/02/17 23:49:01 pk Exp $ */ /* * Copyright (c) 1988, 1989, 1990, 1993 @@ -37,14 +37,14 @@ */ #ifdef MAKE_BOOTSTRAP -static char rcsid[] = "$NetBSD: lstForEachFrom.c,v 1.7 1997/09/28 03:31:26 lukem Exp $"; +static char rcsid[] = "$NetBSD: lstForEachFrom.c,v 1.8 2002/02/17 23:49:01 pk Exp $"; #else #include #ifndef lint #if 0 static char sccsid[] = "@(#)lstForEachFrom.c 8.1 (Berkeley) 6/6/93"; #else -__RCSID("$NetBSD: lstForEachFrom.c,v 1.7 1997/09/28 03:31:26 lukem Exp $"); +__RCSID("$NetBSD: lstForEachFrom.c,v 1.8 2002/02/17 23:49:01 pk Exp $"); #endif #endif /* not lint */ #endif @@ -98,20 +98,28 @@ Lst_ForEachFrom (l, ln, proc, d) next = tln->nextPtr; + /* + * We're done with the traversal if + * - the next node to examine is the first in the queue or + * doesn't exist and + * - nothing's been added after the current node (check this + * after proc() has been called). + */ + done = (next == NilListNode || next == list->firstPtr); + (void) tln->useCount++; result = (*proc) (tln->datum, d); (void) tln->useCount--; /* - * We're done with the traversal if - * - nothing's been added after the current node and - * - the next node to examine is the first in the queue or - * doesn't exist. + * Now check whether a node has been added. + * Note: this doesn't work if this node was deleted before + * the new node was added. */ - done = (next == tln->nextPtr && - (next == NilListNode || next == list->firstPtr)); - - next = tln->nextPtr; + if (next != tln->nextPtr) { + next = tln->nextPtr; + done = 0; + } if (tln->flags & LN_DELETED) { free((char *)tln);