summaryrefslogtreecommitdiffstats
path: root/bin/ksh/expr.c
diff options
context:
space:
mode:
authorschwarze <schwarze@openbsd.org>2019-02-20 23:59:17 +0000
committerschwarze <schwarze@openbsd.org>2019-02-20 23:59:17 +0000
commit0ac57757bcdd34e94e3e2d3b05ea4601a6b10e8a (patch)
treef6a00a43dd03071d9f0e35053410e7c95c198f6b /bin/ksh/expr.c
parentAdd a test that catches problems of tcp md5 signatures, like the one (diff)
downloadwireguard-openbsd-0ac57757bcdd34e94e3e2d3b05ea4601a6b10e8a.tar.xz
wireguard-openbsd-0ac57757bcdd34e94e3e2d3b05ea4601a6b10e8a.zip
When evaluating an arithmetical expression, for example inside $(()),
never do substitution (neither parameter, nor command, nor arithmetic, nor tilde substitution) on the values of any variables encountered inside the expression, but do recursively perform arithmetical evaluation of subexpressions as required. This makes behaviour more consistent, without hindering any behaviour promised in the manual page. A quirk originally reported by Andy Chu <andychup at gmail dot com> was that in the past, when encountering an array index, the shell would not only do evaluation, but also substitution on the array index, even though substitution would not be done on the expression in general. tobias@ contributed to initial efforts of understanding the quirk. patch tested in a bulk build by naddy@ "please commit" deraadt@
Diffstat (limited to 'bin/ksh/expr.c')
-rw-r--r--bin/ksh/expr.c7
1 files changed, 6 insertions, 1 deletions
diff --git a/bin/ksh/expr.c b/bin/ksh/expr.c
index fd2f4be935c..782de57ebec 100644
--- a/bin/ksh/expr.c
+++ b/bin/ksh/expr.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: expr.c,v 1.33 2018/04/09 17:53:36 tobias Exp $ */
+/* $OpenBSD: expr.c,v 1.34 2019/02/20 23:59:17 schwarze Exp $ */
/*
* Korn expression evaluation
@@ -170,6 +170,7 @@ v_evaluate(struct tbl *vp, const char *expr, volatile int error_ok,
struct tbl *v;
Expr_state curstate;
Expr_state * const es = &curstate;
+ int save_disable_subst;
int i;
/* save state to allow recursive calls */
@@ -180,8 +181,10 @@ v_evaluate(struct tbl *vp, const char *expr, volatile int error_ok,
curstate.val = NULL;
newenv(E_ERRH);
+ save_disable_subst = disable_subst;
i = sigsetjmp(genv->jbuf, 0);
if (i) {
+ disable_subst = save_disable_subst;
/* Clear EXPRINEVAL in of any variables we were playing with */
if (curstate.evaling)
curstate.evaling->flag &= ~EXPRINEVAL;
@@ -588,7 +591,9 @@ intvar(Expr_state *es, struct tbl *vp)
evalerr(es, ET_RECURSIVE, vp->name);
es->evaling = vp;
vp->flag |= EXPRINEVAL;
+ disable_subst++;
v_evaluate(vq, str_val(vp), KSH_UNWIND_ERROR, es->arith);
+ disable_subst--;
vp->flag &= ~EXPRINEVAL;
es->evaling = NULL;
}