Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

[PATCH] honour NO_UNSET when reading values in arithmetic expansion/commands



Op 21-12-18 om 07:53 schreef Bart Schaefer:
On Thu, Dec 20, 2018 at 2:48 PM Martijn Dekker <martijn@xxxxxxxx> wrote:

However, I think having 'set -u' apply to $(( x )) is "obvious" and
useful behaviour.

Reasonable.

This should make that happen, if I did it right. All the regression tests pass, including three added ones.

With this patch, zsh throws an error but does not exit on an unset variable in the ((arithmetic command)) or in 'let'. But it doesn't exit on a syntax error either, so that is consistent. It exits as expected on an unset variable in $((arithmetic expansion)).

- M.

diff --git a/Doc/Zsh/options.yo b/Doc/Zsh/options.yo
index 25b3d57..bc182eb 100644
--- a/Doc/Zsh/options.yo
+++ b/Doc/Zsh/options.yo
@@ -753,7 +753,9 @@ pindex(NOUNSET)
 cindex(parameters, substituting unset)
 cindex(unset parameters, substituting)
 item(tt(UNSET) (tt(PLUS()u), ksh: tt(PLUS()u)) <K> <S> <Z>)(
-Treat unset parameters as if they were empty when substituting.
+Treat unset parameters as if they were empty when substituting, and as if
+they were zero when reading their values in arithmetic expansion and
+arithmetic commands.
 Otherwise they are treated as an error.
 )
 pindex(WARN_CREATE_GLOBAL)
diff --git a/README b/README
index aaaee50..0002104 100644
--- a/README
+++ b/README
@@ -42,6 +42,10 @@ array.
 The gen-applied-string hook is unaffected; it still receives the patches in
 reverse order, from last applied to first applied.
 
+2) The option NO_UNSET now also applies when reading values from
+variables without a preceding '$' sign in shell arithmetic expansion
+and in the double-parentheses and 'let' arithmetic commands.
+
 Incompatibilities between 5.5.1 and 5.6.2
 ------------------------------------------
 
diff --git a/Src/math.c b/Src/math.c
index b08e05c..a387700 100644
--- a/Src/math.c
+++ b/Src/math.c
@@ -342,6 +342,8 @@ getmathparam(struct mathvalue *mptr)
 	mptr->pval = (Value)zhalloc(sizeof(struct value));
 	if (!getvalue(mptr->pval, &s, 1))
 	{
+	    if (unset(UNSET))
+		zerr("%s: parameter not set", mptr->lval);
 	    mptr->pval = NULL;
 	    if (isset(FORCEFLOAT)) {
 		result.type = MN_FLOAT;
diff --git a/Test/C01arith.ztst b/Test/C01arith.ztst
index f1364ab..9dfc065 100644
--- a/Test/C01arith.ztst
+++ b/Test/C01arith.ztst
@@ -471,3 +471,19 @@
   print $(( -2#101-16#f ))
 0: Unary minus doesn't apply to base but to number as a whole.
 >-20
+
+  ( set -o nounset
+  true $(( noexist + 1 ))
+  echo 'should never get here' )
+1:Arithmetic, NO_UNSET part 1
+?(eval):2: noexist: parameter not set
+
+  ( setopt nounset
+  (( noexist++ )) )
+2:Arithmetic, NO_UNSET part 2
+?(eval):2: noexist: parameter not set
+
+  ( unsetopt unset
+  let noexist==0 )
+1:Arithmetic, NO_UNSET part 3
+?(eval):2: noexist: parameter not set


Messages sorted by: Reverse Date, Date, Thread, Author