Module Name: src Committed By: rillig Date: Fri Dec 10 23:12:44 UTC 2021
Modified Files: src/usr.bin/make: cond.c src/usr.bin/make/unit-tests: cond-eof.exp cond-eof.mk cond-op.exp cond-op.mk Log Message: 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. To generate a diff of this commit: cvs rdiff -u -r1.285 -r1.286 src/usr.bin/make/cond.c cvs rdiff -u -r1.2 -r1.3 src/usr.bin/make/unit-tests/cond-eof.exp \ src/usr.bin/make/unit-tests/cond-eof.mk cvs rdiff -u -r1.10 -r1.11 src/usr.bin/make/unit-tests/cond-op.exp cvs rdiff -u -r1.14 -r1.15 src/usr.bin/make/unit-tests/cond-op.mk Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.bin/make/cond.c diff -u src/usr.bin/make/cond.c:1.285 src/usr.bin/make/cond.c:1.286 --- src/usr.bin/make/cond.c:1.285 Fri Dec 10 19:47:20 2021 +++ src/usr.bin/make/cond.c Fri Dec 10 23:12:44 2021 @@ -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 @@ CondParser_And(CondParser *par, bool doE 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); Index: src/usr.bin/make/unit-tests/cond-eof.exp diff -u src/usr.bin/make/unit-tests/cond-eof.exp:1.2 src/usr.bin/make/unit-tests/cond-eof.exp:1.3 --- src/usr.bin/make/unit-tests/cond-eof.exp:1.2 Mon Dec 14 20:28:09 2020 +++ src/usr.bin/make/unit-tests/cond-eof.exp Fri Dec 10 23:12:44 2021 @@ -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 Index: src/usr.bin/make/unit-tests/cond-eof.mk diff -u src/usr.bin/make/unit-tests/cond-eof.mk:1.2 src/usr.bin/make/unit-tests/cond-eof.mk:1.3 --- src/usr.bin/make/unit-tests/cond-eof.mk:1.2 Mon Dec 14 20:28:09 2020 +++ src/usr.bin/make/unit-tests/cond-eof.mk Fri Dec 10 23:12:44 2021 @@ -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_EFFECT= ${:!echo 'side effect' 1>&2 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} Index: src/usr.bin/make/unit-tests/cond-op.exp diff -u src/usr.bin/make/unit-tests/cond-op.exp:1.10 src/usr.bin/make/unit-tests/cond-op.exp:1.11 --- src/usr.bin/make/unit-tests/cond-op.exp:1.10 Fri Dec 10 20:22:54 2021 +++ src/usr.bin/make/unit-tests/cond-op.exp Fri Dec 10 23:12:44 2021 @@ -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 Index: src/usr.bin/make/unit-tests/cond-op.mk diff -u src/usr.bin/make/unit-tests/cond-op.mk:1.14 src/usr.bin/make/unit-tests/cond-op.mk:1.15 --- src/usr.bin/make/unit-tests/cond-op.mk:1.14 Fri Dec 10 20:22:54 2021 +++ src/usr.bin/make/unit-tests/cond-op.mk Fri Dec 10 23:12:44 2021 @@ -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. +# +# 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. +# +# +# +# +# # -# 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. -# -# 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.