Sat Oct 31 11:34:30 2020 UTC ()
make(1): reduce the scope where recursive expressions are detected

Only the call to Var_Subst needs to be protected since the other
functions have nothing to do with expanding variables.


(rillig)
diff -r1.417 -r1.418 src/usr.bin/make/main.c
diff -r1.614 -r1.615 src/usr.bin/make/var.c

cvs diff -r1.417 -r1.418 src/usr.bin/make/main.c (expand / switch to unified diff)

--- src/usr.bin/make/main.c 2020/10/31 09:35:58 1.417
+++ src/usr.bin/make/main.c 2020/10/31 11:34:30 1.418
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: main.c,v 1.417 2020/10/31 09:35:58 rillig Exp $ */ 1/* $NetBSD: main.c,v 1.418 2020/10/31 11:34:30 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1988, 1989, 1990, 1993 4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to Berkeley by 7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor. 8 * Adam de Boor.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -108,27 +108,27 @@ @@ -108,27 +108,27 @@
108 108
109#include <errno.h> 109#include <errno.h>
110#include <signal.h> 110#include <signal.h>
111#include <stdarg.h> 111#include <stdarg.h>
112#include <time.h> 112#include <time.h>
113 113
114#include "make.h" 114#include "make.h"
115#include "dir.h" 115#include "dir.h"
116#include "job.h" 116#include "job.h"
117#include "pathnames.h" 117#include "pathnames.h"
118#include "trace.h" 118#include "trace.h"
119 119
120/* "@(#)main.c 8.3 (Berkeley) 3/19/94" */ 120/* "@(#)main.c 8.3 (Berkeley) 3/19/94" */
121MAKE_RCSID("$NetBSD: main.c,v 1.417 2020/10/31 09:35:58 rillig Exp $"); 121MAKE_RCSID("$NetBSD: main.c,v 1.418 2020/10/31 11:34:30 rillig Exp $");
122#if defined(MAKE_NATIVE) && !defined(lint) 122#if defined(MAKE_NATIVE) && !defined(lint)
123__COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 " 123__COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 "
124 "The Regents of the University of California. " 124 "The Regents of the University of California. "
125 "All rights reserved."); 125 "All rights reserved.");
126#endif 126#endif
127 127
128#ifndef DEFMAXLOCAL 128#ifndef DEFMAXLOCAL
129#define DEFMAXLOCAL DEFMAXJOBS 129#define DEFMAXLOCAL DEFMAXJOBS
130#endif 130#endif
131 131
132CmdOpts opts; 132CmdOpts opts;
133time_t now; /* Time at start of make */ 133time_t now; /* Time at start of make */
134GNode *DEFAULT; /* .DEFAULT node */ 134GNode *DEFAULT; /* .DEFAULT node */
@@ -1817,27 +1817,27 @@ Error(const char *fmt, ...) @@ -1817,27 +1817,27 @@ Error(const char *fmt, ...)
1817 fprintf(err_file, "%s: ", progname); 1817 fprintf(err_file, "%s: ", progname);
1818 (void)vfprintf(err_file, fmt, ap); 1818 (void)vfprintf(err_file, fmt, ap);
1819 va_end(ap); 1819 va_end(ap);
1820 (void)fprintf(err_file, "\n"); 1820 (void)fprintf(err_file, "\n");
1821 (void)fflush(err_file); 1821 (void)fflush(err_file);
1822 if (err_file == stderr) 1822 if (err_file == stderr)
1823 break; 1823 break;
1824 err_file = stderr; 1824 err_file = stderr;
1825 } 1825 }
1826} 1826}
1827 1827
1828/* Produce a Fatal error message, then exit immediately. 1828/* Produce a Fatal error message, then exit immediately.
1829 * 1829 *
1830 * If jobs are running, waits for them to finish. */ 1830 * If jobs are running, wait for them to finish. */
1831void 1831void
1832Fatal(const char *fmt, ...) 1832Fatal(const char *fmt, ...)
1833{ 1833{
1834 va_list ap; 1834 va_list ap;
1835 1835
1836 va_start(ap, fmt); 1836 va_start(ap, fmt);
1837 if (jobsRunning) 1837 if (jobsRunning)
1838 Job_Wait(); 1838 Job_Wait();
1839 1839
1840 (void)fflush(stdout); 1840 (void)fflush(stdout);
1841 (void)vfprintf(stderr, fmt, ap); 1841 (void)vfprintf(stderr, fmt, ap);
1842 va_end(ap); 1842 va_end(ap);
1843 (void)fprintf(stderr, "\n"); 1843 (void)fprintf(stderr, "\n");

cvs diff -r1.614 -r1.615 src/usr.bin/make/var.c (expand / switch to unified diff)

--- src/usr.bin/make/var.c 2020/10/31 09:57:47 1.614
+++ src/usr.bin/make/var.c 2020/10/31 11:34:30 1.615
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: var.c,v 1.614 2020/10/31 09:57:47 rillig Exp $ */ 1/* $NetBSD: var.c,v 1.615 2020/10/31 11:34:30 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1988, 1989, 1990, 1993 4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to Berkeley by 7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor. 8 * Adam de Boor.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -119,27 +119,27 @@ @@ -119,27 +119,27 @@
119#include <sys/types.h> 119#include <sys/types.h>
120#include <regex.h> 120#include <regex.h>
121#endif 121#endif
122#include <inttypes.h> 122#include <inttypes.h>
123#include <limits.h> 123#include <limits.h>
124#include <time.h> 124#include <time.h>
125 125
126#include "make.h" 126#include "make.h"
127#include "dir.h" 127#include "dir.h"
128#include "job.h" 128#include "job.h"
129#include "metachar.h" 129#include "metachar.h"
130 130
131/* "@(#)var.c 8.3 (Berkeley) 3/19/94" */ 131/* "@(#)var.c 8.3 (Berkeley) 3/19/94" */
132MAKE_RCSID("$NetBSD: var.c,v 1.614 2020/10/31 09:57:47 rillig Exp $"); 132MAKE_RCSID("$NetBSD: var.c,v 1.615 2020/10/31 11:34:30 rillig Exp $");
133 133
134#define VAR_DEBUG1(fmt, arg1) DEBUG1(VAR, fmt, arg1) 134#define VAR_DEBUG1(fmt, arg1) DEBUG1(VAR, fmt, arg1)
135#define VAR_DEBUG2(fmt, arg1, arg2) DEBUG2(VAR, fmt, arg1, arg2) 135#define VAR_DEBUG2(fmt, arg1, arg2) DEBUG2(VAR, fmt, arg1, arg2)
136#define VAR_DEBUG3(fmt, arg1, arg2, arg3) DEBUG3(VAR, fmt, arg1, arg2, arg3) 136#define VAR_DEBUG3(fmt, arg1, arg2, arg3) DEBUG3(VAR, fmt, arg1, arg2, arg3)
137#define VAR_DEBUG4(fmt, arg1, arg2, arg3, arg4) DEBUG4(VAR, fmt, arg1, arg2, arg3, arg4) 137#define VAR_DEBUG4(fmt, arg1, arg2, arg3, arg4) DEBUG4(VAR, fmt, arg1, arg2, arg3, arg4)
138 138
139ENUM_FLAGS_RTTI_3(VarEvalFlags, 139ENUM_FLAGS_RTTI_3(VarEvalFlags,
140 VARE_UNDEFERR, VARE_WANTRES, VARE_ASSIGN); 140 VARE_UNDEFERR, VARE_WANTRES, VARE_ASSIGN);
141 141
142/* 142/*
143 * This lets us tell if we have replaced the original environ 143 * This lets us tell if we have replaced the original environ
144 * (which we cannot free). 144 * (which we cannot free).
145 */ 145 */
@@ -3652,54 +3652,50 @@ Var_Parse(const char **pp, GNode *ctxt,  @@ -3652,54 +3652,50 @@ Var_Parse(const char **pp, GNode *ctxt,
3652 * Most modifiers leave this expression in the "undefined" state 3652 * Most modifiers leave this expression in the "undefined" state
3653 * (VEF_UNDEF), only a few modifiers like :D, :U, :L, :P turn this 3653 * (VEF_UNDEF), only a few modifiers like :D, :U, :L, :P turn this
3654 * undefined expression into a defined expression (VEF_DEF). 3654 * undefined expression into a defined expression (VEF_DEF).
3655 * 3655 *
3656 * At the end, after applying all modifiers, if the expression 3656 * At the end, after applying all modifiers, if the expression
3657 * is still undefined, Var_Parse will return an empty string 3657 * is still undefined, Var_Parse will return an empty string
3658 * instead of the actually computed value. */ 3658 * instead of the actually computed value. */
3659 v = VarNew(varname, varname, "", 0); 3659 v = VarNew(varname, varname, "", 0);
3660 exprFlags = VEF_UNDEF; 3660 exprFlags = VEF_UNDEF;
3661 } else 3661 } else
3662 free(varname); 3662 free(varname);
3663 } 3663 }
3664 3664
3665 if (v->flags & VAR_IN_USE) { 3665 if (v->flags & VAR_IN_USE)
3666 Fatal("Variable %s is recursive.", v->name); 3666 Fatal("Variable %s is recursive.", v->name);
3667 /*NOTREACHED*/ 
3668 } else { 
3669 v->flags |= VAR_IN_USE; 
3670 } 
3671 3667
3672 /* 3668 /*
3673 * Before doing any modification, we have to make sure the value 3669 * Before doing any modification, we have to make sure the value
3674 * has been fully expanded. If it looks like recursion might be 3670 * has been fully expanded. If it looks like recursion might be
3675 * necessary (there's a dollar sign somewhere in the variable's value) 3671 * necessary (there's a dollar sign somewhere in the variable's value)
3676 * we just call Var_Subst to do any other substitutions that are 3672 * we just call Var_Subst to do any other substitutions that are
3677 * necessary. Note that the value returned by Var_Subst will have 3673 * necessary. Note that the value returned by Var_Subst will have
3678 * been dynamically-allocated, so it will need freeing when we 3674 * been dynamically-allocated, so it will need freeing when we
3679 * return. 3675 * return.
3680 */ 3676 */
3681 nstr = Buf_GetAll(&v->val, NULL); 3677 nstr = Buf_GetAll(&v->val, NULL);
3682 if (strchr(nstr, '$') != NULL && (eflags & VARE_WANTRES)) { 3678 if (strchr(nstr, '$') != NULL && (eflags & VARE_WANTRES)) {
3683 VarEvalFlags nested_eflags = eflags; 3679 VarEvalFlags nested_eflags = eflags;
3684 if (DEBUG(LINT)) 3680 if (DEBUG(LINT))
3685 nested_eflags &= ~(unsigned)VARE_UNDEFERR; 3681 nested_eflags &= ~(unsigned)VARE_UNDEFERR;
 3682 v->flags |= VAR_IN_USE;
3686 (void)Var_Subst(nstr, ctxt, nested_eflags, &nstr); 3683 (void)Var_Subst(nstr, ctxt, nested_eflags, &nstr);
 3684 v->flags &= ~(unsigned)VAR_IN_USE;
3687 /* TODO: handle errors */ 3685 /* TODO: handle errors */
3688 *freePtr = nstr; 3686 *freePtr = nstr;
3689 } 3687 }
3690 3688
3691 v->flags &= ~(unsigned)VAR_IN_USE; 
3692 
3693 if (haveModifier || extramodifiers != NULL) { 3689 if (haveModifier || extramodifiers != NULL) {
3694 void *extraFree; 3690 void *extraFree;
3695 3691
3696 extraFree = NULL; 3692 extraFree = NULL;
3697 if (extramodifiers != NULL) { 3693 if (extramodifiers != NULL) {
3698 const char *em = extramodifiers; 3694 const char *em = extramodifiers;
3699 nstr = ApplyModifiers(&em, nstr, '(', ')', 3695 nstr = ApplyModifiers(&em, nstr, '(', ')',
3700 v, &exprFlags, ctxt, eflags, &extraFree); 3696 v, &exprFlags, ctxt, eflags, &extraFree);
3701 } 3697 }
3702 3698
3703 if (haveModifier) { 3699 if (haveModifier) {
3704 /* Skip initial colon. */ 3700 /* Skip initial colon. */
3705 p++; 3701 p++;