From dca20086146e7c6022e394cc760ad39b12d96db2 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sun, 7 Mar 2021 17:07:06 +0000 Subject: [PATCH 2/2] zmathfunc: Fix bug where the exit code would be non-zero if the expression evaluted to zero. --- Functions/Math/zmathfunc | 10 ++++++++-- Test/Z02zmathfunc.ztst | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) 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