In bash, `return -1` is processed as `return 255`
Steps to reproduce
This report is possibly not a bug.
In bash, any single integer given as an argument to the return
builtin gets bitwise anded with 255 to obtain a final exit code. So, if any specific actions are taken depending on these exit statuses returning as '-1', then some unexpected behavior may be occurring. In zsh, return -1
appears to actually produce an error code of "-1". (credit: shellcheck SC2152)
Files:
- ./graphviz/ci/macos-install-build-dependencies.sh
- ./graphviz/ci/macos-install-runtime-dependencies.sh
Info from existing files:
[liveuser@localhost-live]$ grep -rin 'return -1' ./graphviz | grep -F '.sh:'
./graphviz/ci/macos-install-build-dependencies.sh:77: return -1 ./graphviz/ci/macos-install-runtime-dependencies.sh:59: return -1
[liveuser@localhost-live]$ head -n1 ./graphviz/ci/macos-install-build-dependencies.sh ./graphviz/ci/macos-install-runtime-dependencies.sh
==> ./graphviz/ci/macos-install-build-dependencies.sh <== #!/usr/bin/env bash
==> ./graphviz/ci/macos-install-runtime-dependencies.sh <== #!/usr/bin/env bash
bash source, function get_exitstat
[liveuser@localhost-live]$ vim +578 ./bash/builtins/common.c
578 arg = list->word->word; 579 if (arg == 0 || legal_number (arg, &sval) == 0) 580 { 581 sh_neednumarg (list->word->word ? list->word->word : "`'"); 582 return EX_BADUSAGE; 583 } 584 no_args (list->next); 585 586 status = sval & 255; /* <<< */ 587 return status; 588 }
Effects
bash:
[liveuser@localhost-live]$ bash -c 'echo $BASH_VERSION'; set -x; foo(){ echo bar; return -1;}; foo; echo $?; set -
5.2.15(1)-release
- foo
- echo bar bar
- return -1
- echo 255 255
- set -
zsh:
[liveuser@localhost-live]% zsh -c 'echo $ZSH_VERSION'; set -x; foo(){ echo bar; return -1;}; foo; echo $?; set -
5.9 +zsh:1> foo +foo:0> echo bar bar +foo:0> return -1 +zsh:1> echo -1 -1 +zsh:1> set -
ksh93:
[liveuser@localhost-live]$ ksh -c 'echo $KSH_VERSION'; set -x; foo(){ echo bar; return -1;}; foo; echo $?; set -
Version AJM 93u+m/1.0.4 2022-10-22
- foo
- echo bar bar
- return -1
- echo 255 255
- set -
mksh:
[liveuser@localhost-live]$ ksh -c 'echo $KSH_VERSION'; set -x; foo(){ echo bar; return -1;}; foo; echo $?; set -
@(#)MIRBSD KSH R59 2020/10/31
- foo
- echo bar bar
- return -1
- echo 255 255
- set -
dash
[liveuser@localhost-live]$ rpm -qf /usr/bin/dash; set -x; foo(){ echo bar; return -1;}; foo; echo $?; set -
dash-0.5.11.5-3.fc38.x86_64
- foo
- echo bar bar
- return -1
- echo 255 255
- set -
OS Version
[liveuser@localhost-live]$ uname -a Linux localhost-live 6.2.9-300.fc38.x86_64 #1 (closed) SMP PREEMPT_DYNAMIC Thu Mar 30 22:32:58 UTC 2023 x86_64 GNU/Linux
Graphviz Version
[liveuser@localhost-live]$ dot -V dot - graphviz version 7.1.0 (0)