make(1): make list library code stricter

Up to now, the list library didn't distinguish between programming
mistakes (violations of invariants, illegal parameter values) and
actually interesting situations like "element not found in list".

The current code contains many branches for conditions that are neither
exercised by the unit tests nor by real-world usage.  There is no point
in keeping this unnecessary code.

The list functions will be migrated from their lenient variants to the
stricter variants in small parts, each function getting the S suffix
when it is made strict, to avoid any confusion about how strict a
particular function is.  When all functions have been migrated, they
will be renamed back to their original names.

While here, the comments of the functions are cleaned up since they
mention irrelevant implementation details in the API comments, as well
as "side effects" that are really main effects.
This commit is contained in:
rillig 2020-08-21 03:36:03 +00:00
parent 001a378b00
commit 7b0da2fa03
7 changed files with 46 additions and 80 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: compat.c,v 1.118 2020/08/01 14:47:49 rillig Exp $ */
/* $NetBSD: compat.c,v 1.119 2020/08/21 03:36:03 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@ -70,14 +70,14 @@
*/
#ifndef MAKE_NATIVE
static char rcsid[] = "$NetBSD: compat.c,v 1.118 2020/08/01 14:47:49 rillig Exp $";
static char rcsid[] = "$NetBSD: compat.c,v 1.119 2020/08/21 03:36:03 rillig Exp $";
#else
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)compat.c 8.2 (Berkeley) 3/19/94";
#else
__RCSID("$NetBSD: compat.c,v 1.118 2020/08/01 14:47:49 rillig Exp $");
__RCSID("$NetBSD: compat.c,v 1.119 2020/08/21 03:36:03 rillig Exp $");
#endif
#endif /* not lint */
#endif
@ -249,7 +249,7 @@ CompatRunCommand(void *cmdp, void *gnp)
return 0;
}
cmd = cmdStart;
Lst_Replace(cmdNode, cmdStart);
Lst_ReplaceS(cmdNode, cmdStart);
if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) {
(void)Lst_AtEnd(ENDNode->commands, cmdStart);
@ -400,7 +400,7 @@ again:
free(mav);
free(bp);
Lst_Replace(cmdNode, NULL);
Lst_ReplaceS(cmdNode, NULL);
#ifdef USE_META
if (useMeta) {

View File

@ -1,4 +1,4 @@
/* $NetBSD: dir.c,v 1.93 2020/08/21 02:20:47 rillig Exp $ */
/* $NetBSD: dir.c,v 1.94 2020/08/21 03:36:03 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@ -70,14 +70,14 @@
*/
#ifndef MAKE_NATIVE
static char rcsid[] = "$NetBSD: dir.c,v 1.93 2020/08/21 02:20:47 rillig Exp $";
static char rcsid[] = "$NetBSD: dir.c,v 1.94 2020/08/21 03:36:03 rillig Exp $";
#else
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)dir.c 8.2 (Berkeley) 1/2/94";
#else
__RCSID("$NetBSD: dir.c,v 1.93 2020/08/21 02:20:47 rillig Exp $");
__RCSID("$NetBSD: dir.c,v 1.94 2020/08/21 03:36:03 rillig Exp $");
#endif
#endif /* not lint */
#endif
@ -429,7 +429,7 @@ Dir_InitDot(void)
/* Remove old entry from openDirectories, but do not destroy. */
ln = Lst_Member(openDirectories, dot);
(void)Lst_Remove(openDirectories, ln);
Lst_RemoveS(openDirectories, ln);
}
dot = Dir_AddDir(NULL, ".");
@ -1719,7 +1719,7 @@ Dir_Destroy(void *pp)
LstNode ln;
ln = Lst_Member(openDirectories, p);
(void)Lst_Remove(openDirectories, ln);
Lst_RemoveS(openDirectories, ln);
Hash_DeleteTable(&p->files);
free(p->name);

View File

@ -1,4 +1,4 @@
/* $NetBSD: lst.c,v 1.7 2020/08/21 03:03:45 rillig Exp $ */
/* $NetBSD: lst.c,v 1.8 2020/08/21 03:36:03 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@ -32,15 +32,17 @@
* SUCH DAMAGE.
*/
#include <assert.h>
#include "lst.h"
#include "make_malloc.h"
#ifndef MAKE_NATIVE
static char rcsid[] = "$NetBSD: lst.c,v 1.7 2020/08/21 03:03:45 rillig Exp $";
static char rcsid[] = "$NetBSD: lst.c,v 1.8 2020/08/21 03:36:03 rillig Exp $";
#else
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: lst.c,v 1.7 2020/08/21 03:03:45 rillig Exp $");
__RCSID("$NetBSD: lst.c,v 1.8 2020/08/21 03:36:03 rillig Exp $");
#endif /* not lint */
#endif
@ -405,30 +407,16 @@ Lst_AtEnd(Lst l, void *d)
return Lst_InsertAfter(l, end, d);
}
/*-
*-----------------------------------------------------------------------
* Lst_Remove --
* Remove the given node from the given list.
*
* Results:
* SUCCESS or FAILURE.
*
* Side Effects:
* The list's firstPtr will be set to NULL if ln is the last
* node on the list. firsPtr and lastPtr will be altered if ln is
* either the first or last node, respectively, on the list.
*
*-----------------------------------------------------------------------
*/
ReturnStatus
Lst_Remove(Lst l, LstNode ln)
/* Remove the given node from the given list.
* The datum stored in the node must be freed by the caller, if necessary. */
void
Lst_RemoveS(Lst l, LstNode ln)
{
List list = l;
ListNode lNode = ln;
if (!LstValid(l) || !LstNodeValid(ln)) {
return FAILURE;
}
assert(LstValid(l));
assert(LstNodeValid(ln));
/*
* unlink it from the list
@ -473,32 +461,13 @@ Lst_Remove(Lst l, LstNode ln)
} else {
lNode->deleted = TRUE;
}
return SUCCESS;
}
/*-
*-----------------------------------------------------------------------
* Lst_Replace --
* Replace the datum in the given node with the new datum
*
* Results:
* SUCCESS or FAILURE.
*
* Side Effects:
* The datum field fo the node is altered.
*
*-----------------------------------------------------------------------
*/
ReturnStatus
Lst_Replace(LstNode ln, void *d)
/* Replace the datum in the given node with the new datum. */
void
Lst_ReplaceS(LstNode ln, void *d)
{
if (ln == NULL) {
return FAILURE;
} else {
(ln)->datum = d;
return SUCCESS;
}
ln->datum = d;
}
@ -1080,9 +1049,6 @@ Lst_DeQueue(Lst l)
}
rd = tln->datum;
if (Lst_Remove(l, tln) == FAILURE) {
return NULL;
} else {
return rd;
}
Lst_RemoveS(l, tln);
return rd;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: lst.h,v 1.23 2020/08/21 02:56:25 rillig Exp $ */
/* $NetBSD: lst.h,v 1.24 2020/08/21 03:36:03 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@ -122,9 +122,9 @@ ReturnStatus Lst_AtFront(Lst, void *);
/* Place an element at the end of a lst. */
ReturnStatus Lst_AtEnd(Lst, void *);
/* Remove an element */
ReturnStatus Lst_Remove(Lst, LstNode);
void Lst_RemoveS(Lst, LstNode);
/* Replace a node with a new value */
ReturnStatus Lst_Replace(LstNode, void *);
void Lst_ReplaceS(LstNode, void *);
/* Concatenate two lists */
ReturnStatus Lst_Concat(Lst, Lst, int);

View File

@ -1,4 +1,4 @@
/* $NetBSD: make.c,v 1.104 2020/08/21 02:20:47 rillig Exp $ */
/* $NetBSD: make.c,v 1.105 2020/08/21 03:36:03 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@ -69,14 +69,14 @@
*/
#ifndef MAKE_NATIVE
static char rcsid[] = "$NetBSD: make.c,v 1.104 2020/08/21 02:20:47 rillig Exp $";
static char rcsid[] = "$NetBSD: make.c,v 1.105 2020/08/21 03:36:03 rillig Exp $";
#else
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)make.c 8.1 (Berkeley) 6/6/93";
#else
__RCSID("$NetBSD: make.c,v 1.104 2020/08/21 02:20:47 rillig Exp $");
__RCSID("$NetBSD: make.c,v 1.105 2020/08/21 03:36:03 rillig Exp $");
#endif
#endif /* not lint */
#endif
@ -548,7 +548,7 @@ MakeHandleUse(void *cgnp, void *pgnp)
* whether to queue the parent or examine its children...
*/
if ((ln = Lst_Member(pgn->children, cgn)) != NULL) {
Lst_Remove(pgn->children, ln);
Lst_RemoveS(pgn->children, ln);
pgn->unmade--;
}
return 0;

View File

@ -1,4 +1,4 @@
/* $NetBSD: meta.c,v 1.93 2020/08/21 02:20:47 rillig Exp $ */
/* $NetBSD: meta.c,v 1.94 2020/08/21 03:36:03 rillig Exp $ */
/*
* Implement 'meta' mode.
@ -1341,7 +1341,7 @@ meta_oodate(GNode *gn, Boolean oodate)
nln = Lst_FindFrom(missingFiles, Lst_Succ(ln),
p, path_match);
tp = Lst_Datum(ln);
Lst_Remove(missingFiles, ln);
Lst_RemoveS(missingFiles, ln);
free(tp);
} while ((ln = nln) != NULL);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: suff.c,v 1.97 2020/08/21 02:20:48 rillig Exp $ */
/* $NetBSD: suff.c,v 1.98 2020/08/21 03:36:03 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@ -69,14 +69,14 @@
*/
#ifndef MAKE_NATIVE
static char rcsid[] = "$NetBSD: suff.c,v 1.97 2020/08/21 02:20:48 rillig Exp $";
static char rcsid[] = "$NetBSD: suff.c,v 1.98 2020/08/21 03:36:03 rillig Exp $";
#else
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)suff.c 8.4 (Berkeley) 3/21/94";
#else
__RCSID("$NetBSD: suff.c,v 1.97 2020/08/21 02:20:48 rillig Exp $");
__RCSID("$NetBSD: suff.c,v 1.98 2020/08/21 03:36:03 rillig Exp $");
#endif
#endif /* not lint */
#endif
@ -409,7 +409,7 @@ SuffUnRef(void *lp, void *sp)
LstNode ln = Lst_Member(l, sp);
if (ln != NULL) {
Lst_Remove(l, ln);
Lst_RemoveS(l, ln);
((Suff *)sp)->refCount--;
}
}
@ -1311,7 +1311,7 @@ SuffRemoveSrc(Lst l)
#ifdef DEBUG_SRC
LstNode ln2 = Lst_Member(s->parent->cp, s);
if (ln2 != NULL)
Lst_Remove(s->parent->cp, ln2);
Lst_RemoveS(s->parent->cp, ln2);
#endif
--s->parent->children;
}
@ -1319,7 +1319,7 @@ SuffRemoveSrc(Lst l)
fprintf(debug_file, "free: [l=%p] p=%p %d\n", l, s, s->children);
Lst_Destroy(s->cp, NULL);
#endif
Lst_Remove(l, ln);
Lst_RemoveS(l, ln);
free(s);
t |= 1;
Lst_Close(l);
@ -1674,8 +1674,8 @@ SuffExpandChildren(LstNode cln, GNode *pgn)
* keep it from being processed.
*/
pgn->unmade--;
Lst_Remove(pgn->children, cln);
Lst_Remove(cgn->parents, Lst_Member(cgn->parents, pgn));
Lst_RemoveS(pgn->children, cln);
Lst_RemoveS(cgn->parents, Lst_Member(cgn->parents, pgn));
}
static void
@ -1726,8 +1726,8 @@ SuffExpandWildcards(LstNode cln, GNode *pgn)
* keep it from being processed.
*/
pgn->unmade--;
Lst_Remove(pgn->children, cln);
Lst_Remove(cgn->parents, Lst_Member(cgn->parents, pgn));
Lst_RemoveS(pgn->children, cln);
Lst_RemoveS(cgn->parents, Lst_Member(cgn->parents, pgn));
}
/*-