Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/make make: do not evaluate misplaced expressions in ...



details:   https://anonhg.NetBSD.org/src/rev/0a7251664cab
branches:  trunk
changeset: 1027549:0a7251664cab
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Dec 10 23:12:44 2021 +0000

description:
make: do not evaluate misplaced expressions in malformed conditions

This change only affects the behavior for parse errors.  Syntactically
well-formed conditions work exactly as before.

diffstat:

 usr.bin/make/cond.c                  |   6 +++---
 usr.bin/make/unit-tests/cond-eof.exp |   3 ---
 usr.bin/make/unit-tests/cond-eof.mk  |  12 ++++++------
 usr.bin/make/unit-tests/cond-op.exp  |   4 ++--
 usr.bin/make/unit-tests/cond-op.mk   |  34 +++++++++++++++++-----------------
 5 files changed, 28 insertions(+), 31 deletions(-)

diffs (139 lines):

diff -r 3000257bdc00 -r 0a7251664cab usr.bin/make/cond.c
--- a/usr.bin/make/cond.c       Fri Dec 10 20:36:02 2021 +0000
+++ b/usr.bin/make/cond.c       Fri Dec 10 23:12:44 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: cond.c,v 1.285 2021/12/10 19:47:20 rillig Exp $        */
+/*     $NetBSD: cond.c,v 1.286 2021/12/10 23:12:44 rillig Exp $        */
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -95,7 +95,7 @@
 #include "dir.h"
 
 /*     "@(#)cond.c     8.2 (Berkeley) 1/2/94"  */
-MAKE_RCSID("$NetBSD: cond.c,v 1.285 2021/12/10 19:47:20 rillig Exp $");
+MAKE_RCSID("$NetBSD: cond.c,v 1.286 2021/12/10 23:12:44 rillig Exp $");
 
 /*
  * The parsing of conditional expressions is based on this grammar:
@@ -987,7 +987,7 @@
        if (res == CR_ERROR)
                return CR_ERROR;
 
-       op = CondParser_Token(par, doEval);
+       op = CondParser_Token(par, false);
        if (op == TOK_AND) {
                if (res == CR_TRUE)
                        return CondParser_And(par, doEval);
diff -r 3000257bdc00 -r 0a7251664cab usr.bin/make/unit-tests/cond-eof.exp
--- a/usr.bin/make/unit-tests/cond-eof.exp      Fri Dec 10 20:36:02 2021 +0000
+++ b/usr.bin/make/unit-tests/cond-eof.exp      Fri Dec 10 23:12:44 2021 +0000
@@ -1,8 +1,5 @@
-side effect
 make: "cond-eof.mk" line 15: Malformed conditional (0 ${SIDE_EFFECT} ${SIDE_EFFECT2})
-side effect
 make: "cond-eof.mk" line 17: Malformed conditional (1 ${SIDE_EFFECT} ${SIDE_EFFECT2})
-side effect
 make: "cond-eof.mk" line 19: Malformed conditional ((0) ${SIDE_EFFECT} ${SIDE_EFFECT2})
 make: Fatal errors encountered -- cannot continue
 make: stopped in unit-tests
diff -r 3000257bdc00 -r 0a7251664cab usr.bin/make/unit-tests/cond-eof.mk
--- a/usr.bin/make/unit-tests/cond-eof.mk       Fri Dec 10 20:36:02 2021 +0000
+++ b/usr.bin/make/unit-tests/cond-eof.mk       Fri Dec 10 23:12:44 2021 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: cond-eof.mk,v 1.2 2020/12/14 20:28:09 rillig Exp $
+# $NetBSD: cond-eof.mk,v 1.3 2021/12/10 23:12:44 rillig Exp $
 #
 # Tests for parsing conditions, especially the end of such conditions, which
 # are represented as the token TOK_EOF.
@@ -7,11 +7,11 @@
 SIDE_EFFECT2=  ${:!echo 'side effect 2' 1>&2!}
 
 # In the following conditions, ${SIDE_EFFECT} is the position of the first
-# parse error.  It is always fully evaluated, even if it were not necessary
-# to expand the variable expression.  This is because these syntax errors are
-# an edge case that does not occur during normal operation, therefore there
-# is no need to optimize for this case, and it would slow down the common
-# case as well.
+# parse error.  Before cond.c 1.286 from 2021-12-10, it was always fully
+# evaluated, even if it was not necessary to expand the variable expression.
+# These syntax errors are an edge case that does not occur during normal
+# operation.  Still, it is easy to avoid evaluating these expressions, just in
+# case they have side effects.
 .if 0 ${SIDE_EFFECT} ${SIDE_EFFECT2}
 .endif
 .if 1 ${SIDE_EFFECT} ${SIDE_EFFECT2}
diff -r 3000257bdc00 -r 0a7251664cab usr.bin/make/unit-tests/cond-op.exp
--- a/usr.bin/make/unit-tests/cond-op.exp       Fri Dec 10 20:36:02 2021 +0000
+++ b/usr.bin/make/unit-tests/cond-op.exp       Fri Dec 10 23:12:44 2021 +0000
@@ -1,8 +1,8 @@
 make: "cond-op.mk" line 50: Malformed conditional ("!word" == !word)
 make: "cond-op.mk" line 76: Malformed conditional (0 ${ERR::=evaluated})
-make: "cond-op.mk" line 80: After detecting a parse error after 0, the rest is evaluated.
+make: "cond-op.mk" line 80: A misplaced expression after 0 is not evaluated.
 make: "cond-op.mk" line 84: Malformed conditional (1 ${ERR::=evaluated})
-make: "cond-op.mk" line 88: After detecting a parse error after 1, the rest is evaluated.
+make: "cond-op.mk" line 88: A misplaced expression after 1 is not evaluated.
 make: "cond-op.mk" line 92: Parsing continues until here.
 make: "cond-op.mk" line 95: A B C   =>   (A || B) && C   A || B && C   A || (B && C)
 make: "cond-op.mk" line 102: 0 0 0   =>   0               0             0
diff -r 3000257bdc00 -r 0a7251664cab usr.bin/make/unit-tests/cond-op.mk
--- a/usr.bin/make/unit-tests/cond-op.mk        Fri Dec 10 20:36:02 2021 +0000
+++ b/usr.bin/make/unit-tests/cond-op.mk        Fri Dec 10 23:12:44 2021 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: cond-op.mk,v 1.14 2021/12/10 20:22:54 rillig Exp $
+# $NetBSD: cond-op.mk,v 1.15 2021/12/10 23:12:44 rillig Exp $
 #
 # Tests for operators like &&, ||, ! in .if conditions.
 #
@@ -58,34 +58,34 @@
 .  error
 .endif
 
-# As soon as the parser sees the '$', it knows that the condition will
-# be malformed.  Therefore there is no point in evaluating it.
+# In the following malformed conditions, as soon as the parser sees the '$'
+# after the '0' or the '1', it knows that the condition will be malformed.
+# Therefore there is no point in evaluating the misplaced expression.
 #
-# As of 2021-01-20, that part of the condition is evaluated nevertheless,
-# since CondParser_Or just requests the next token, without restricting
-# the token to the expected tokens.  If the parser were to restrict the
-# valid follow tokens for the token "0" to those that can actually produce
-# a correct condition (which in this case would be comparison operators,
-# TOK_AND, TOK_OR or TOK_RPAREN), the variable expression would not have
-# to be evaluated.
+# Before cond.c 1.286 from 2021-12-10, the extra expression was evaluated
+# nevertheless, since CondParser_Or and CondParser_And asked for the expanded
+# next token, even though in this position of the condition, only comparison
+# operators, TOK_AND, TOK_OR or TOK_RPAREN are allowed.
 #
-# This would add a good deal of complexity to the code though, for almost
-# no benefit, especially since most expressions and conditions are side
-# effect free.
+#
+#
+#
+#
+#
 .undef ERR
 .if 0 ${ERR::=evaluated}
 .  error
 .endif
-.if ${ERR:Uundefined} == evaluated
-.  info After detecting a parse error after 0, the rest is evaluated.
+.if ${ERR:Uundefined} == undefined
+.  info A misplaced expression after 0 is not evaluated.
 .endif
 
 .undef ERR
 .if 1 ${ERR::=evaluated}
 .  error
 .endif
-.if ${ERR:Uundefined} == evaluated
-.  info After detecting a parse error after 1, the rest is evaluated.
+.if ${ERR:Uundefined} == undefined
+.  info A misplaced expression after 1 is not evaluated.
 .endif
 
 # Just in case that parsing should ever stop on the first error.



Home | Main Index | Thread Index | Old Index