about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDaniel Shahaf <d.s@daniel.shahaf.name>2021-03-07 17:07:06 +0000
committerDaniel Shahaf <d.s@daniel.shahaf.name>2021-03-07 21:58:13 +0000
commitf87b73e677739970c61eab55d4a3276beba1bd43 (patch)
tree452a62974d359972dd048a2e41c52738f3ef9f2f
parent6a2a8acf09bc39795f18cc60e90a0d4427d36edd (diff)
downloadzsh-f87b73e677739970c61eab55d4a3276beba1bd43.tar.gz
zsh-f87b73e677739970c61eab55d4a3276beba1bd43.tar.xz
zsh-f87b73e677739970c61eab55d4a3276beba1bd43.zip
48147/0002: zmathfunc: Fix bug where the exit code would be non-zero if the expression evaluted to zero.
-rw-r--r--ChangeLog4
-rw-r--r--Functions/Math/zmathfunc10
-rw-r--r--Test/Z02zmathfunc.ztst2
3 files changed, 13 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index d77fb2df5..022e38994 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2021-03-07  Daniel Shahaf  <d.s@daniel.shahaf.name>
 
+	* 48147/0002: Functions/Math/zmathfunc, Test/Z02zmathfunc.ztst:
+	zmathfunc: Fix bug where the exit code would be non-zero if
+	the expression evaluted to zero.
+
 	* 48147/0001: Test/Z02zmathfunc.ztst: tests: Add a unit test for
 	zmathfunc and a regression test for workers/48146 affecting it.
 
diff --git a/Functions/Math/zmathfunc b/Functions/Math/zmathfunc
index 4ff40700d..8e4b78549 100644
--- a/Functions/Math/zmathfunc
+++ b/Functions/Math/zmathfunc
@@ -1,34 +1,40 @@
 #autoload
 
 zsh_math_func_min() {
+  emulate -L zsh
   local result=$1
   shift
   local arg
   for arg ; do
     (( $arg < result )) && result=$arg
   done
-  (( result )) # return
+  (( result ))
+  true # Careful here: `return 0` evaluates an arithmetic expression
 }
 functions -M min 1 -1 zsh_math_func_min # at least one argument
 
 zsh_math_func_max() {
+  emulate -L zsh
   local result=$1
   shift
   local arg
   for arg ; do
     (( $arg > result )) && result=$arg
   done
-  (( result )) # return
+  (( result ))
+  true # Careful here: `return 0` evaluates an arithmetic expression
 }
 functions -M max 1 -1 zsh_math_func_max # at least one argument
 
 zsh_math_func_sum() {
+  emulate -L zsh
   local sum
   local arg
   for arg ; do
     (( sum += $arg ))
   done
   (( sum ))
+  true # Careful here: `return 0` evaluates an arithmetic expression
 }
 functions -M sum 0 -1 zsh_math_func_sum
 
diff --git a/Test/Z02zmathfunc.ztst b/Test/Z02zmathfunc.ztst
index 94bc59576..43a0a0d76 100644
--- a/Test/Z02zmathfunc.ztst
+++ b/Test/Z02zmathfunc.ztst
@@ -15,7 +15,7 @@
   (set -e; echo $(( min(0,   42) )))
   (set -e; echo $(( max(0,  -42) )))
   (set -e; echo $(( sum(42, -42) )))
--f:regression test for ERR_EXIT 
+0:regression test for ERR_EXIT 
 >0
 >0
 >0