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: avoid recursion in CondParser_Or



details:   https://anonhg.NetBSD.org/src/rev/095d5bf8a6a3
branches:  trunk
changeset: 1027184:095d5bf8a6a3
user:      rillig <rillig%NetBSD.org@localhost>
date:      Thu Dec 09 22:25:58 2021 +0000

description:
make: avoid recursion in CondParser_Or

Previously, a long chain of '1 || 1 || 1 || 1 || ...' led to a deep
recursion.  Furhermore, the code didn't match the grammar on superficial
reading: the grammar said "or || and", the code said "and || or".

No functional change.

diffstat:

 usr.bin/make/cond.c |  25 ++++++++++---------------
 1 files changed, 10 insertions(+), 15 deletions(-)

diffs (58 lines):

diff -r 13c5eea52204 -r 095d5bf8a6a3 usr.bin/make/cond.c
--- a/usr.bin/make/cond.c       Thu Dec 09 21:14:50 2021 +0000
+++ b/usr.bin/make/cond.c       Thu Dec 09 22:25:58 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: cond.c,v 1.282 2021/12/09 20:13:09 rillig Exp $        */
+/*     $NetBSD: cond.c,v 1.283 2021/12/09 22:25:58 rillig Exp $        */
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -95,12 +95,11 @@
 #include "dir.h"
 
 /*     "@(#)cond.c     8.2 (Berkeley) 1/2/94"  */
-MAKE_RCSID("$NetBSD: cond.c,v 1.282 2021/12/09 20:13:09 rillig Exp $");
+MAKE_RCSID("$NetBSD: cond.c,v 1.283 2021/12/09 22:25:58 rillig Exp $");
 
 /*
  * The parsing of conditional expressions is based on this grammar:
- *     Or -> And
- *     Or -> Or '||' And
+ *     Or -> And ('||' And)*
  *     And -> Term
  *     And -> And '&&' Term
  *     Term -> Function '(' Argument ')'
@@ -1002,26 +1001,22 @@
 }
 
 /*
- * Or -> And
- * Or -> Or '||' And
+ * Or -> And ('||' And)*
  */
 static CondResult
 CondParser_Or(CondParser *par, bool doEval)
 {
-       CondResult res;
+       CondResult res, r;
        Token op;
 
-       res = CondParser_And(par, doEval);
-       if (res == CR_ERROR)
+       if ((res = CondParser_And(par, doEval)) == CR_ERROR)
                return CR_ERROR;
 
-       op = CondParser_Token(par, doEval);
-       if (op == TOK_OR) {
-               if (res == CR_FALSE)
-                       return CondParser_Or(par, doEval);
-               if (CondParser_Or(par, false) == CR_ERROR)
+       while ((op = CondParser_Token(par, res == CR_FALSE)) == TOK_OR) {
+               if ((r = CondParser_And(par, res == CR_FALSE)) == CR_ERROR)
                        return CR_ERROR;
-               return res;
+               if (r == CR_TRUE)
+                       res = CR_TRUE;
        }
 
        CondParser_PushBack(par, op);



Home | Main Index | Thread Index | Old Index