Skip to content

Commit

Permalink
global (ble/builtin/*): work around "set -eu" in NixOS initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Jan 22, 2022
1 parent 6a946f0 commit 001c595
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 16 deletions.
1 change: 0 additions & 1 deletion ble.pp
Expand Up @@ -328,7 +328,6 @@ function ble/base/.adjust-bash-options {
[[ $2 == shopt ]] || builtin eval -- "$2=\$shopt"
shopt -u nocasematch 2>/dev/null
return 0
fi
} 2>/dev/null # set -x 対策
## @fn ble/base/.restore-bash-options var_set var_shopt
## @param[out] var_set var_shopt
Expand Down
1 change: 1 addition & 0 deletions docs/ChangeLog.md
Expand Up @@ -291,6 +291,7 @@
- menu (`menu-style:desc`): work around xenl quirks for relative cursor movements (reported by telometto) `#D1728` 3e136a6
- global: work around the arithmetic syntax error of `10#` in Bash-5.1 `#D1734` 7545ea3
- global: adjust implementations for Bash 5.2 `patsub_replacement` `#D1738` 4590997
- global (`ble/builtin/*`): work around `set -eu` in NixOS initialization `#D1743` 0000000

## Internal changes and fixes

Expand Down
23 changes: 23 additions & 0 deletions note.txt
Expand Up @@ -5838,6 +5838,29 @@ bash_tips

2022-01-21

* global: protect overridden builtins against "set -eu" (reported by SuperSandro2000) [#D1743]
https://github.com/akinomyoga/ble.sh/issues/169

またもや set -uex でロードできなくなっているのでその修正を行う。同時に$- 及
び $BASHOPTS (bash-4.0 以下では自前で再構築) を持ちて状態保存のコードを単純
化した。これで以前よりは効率はよくなったのではないかと思う (nocasematch の
検索は多少時間がかかるかもしれない)。

次に各 builtin に対して対策コードを導入する事にする。

- done: trap (util.sh)
- done: sleep (util.sh)
- done: bind (decode.sh)
- done: read (edit.sh)
- done: exit (edit.sh)
- done: history (history.sh)

そもそも set -eu をありがたがって使っている時点で余り分かっていない人が書い
ているという気がする。何れにしてもその時点で一般には防御が難しいが、NixOS
の用意したスクリプトでだけ set -eu を有効にするというのであれば、仕方がない
のかもしれない。勝手に builtin を上書きしてそれで問題を起こすのは ble.sh が
悪い。これによって状態保存・復元の overhead が生じるが仕方がない。

* prompt: C-c で history_share が有効でない (reported by SuperSandro2000) [#D1742]
https://github.com/akinomyoga/ble.sh/issues/96#issuecomment-1018101640

Expand Down
10 changes: 2 additions & 8 deletions src/decode.sh
Expand Up @@ -3940,11 +3940,7 @@ function ble/builtin/bind/read-user-settings {
}

function ble/builtin/bind {
local nocasematch=
((_ble_bash>=30100)) &&
shopt -q nocasematch &&
shopt -u nocasematch &&
nocasematch=1
local set shopt; ble/base/.adjust-bash-options set shopt

[[ ! $_ble_attached || $_ble_edit_exec_inside_userspace ]] &&
ble-edit/exec/save-BASH_REMATCH
Expand All @@ -3960,11 +3956,9 @@ function ble/builtin/bind {
ext=2
fi

[[ $nocasematch ]] &&
shopt -s nocasematch

[[ ! $_ble_attached || $_ble_edit_exec_inside_userspace ]] &&
ble-edit/exec/restore-BASH_REMATCH
ble/base/.restore-bash-options set shopt
return "$ext"
}
function bind { ble/builtin/bind "$@"; }
Expand Down
13 changes: 11 additions & 2 deletions src/edit.sh
Expand Up @@ -5296,11 +5296,13 @@ function ble/builtin/exit {
return 1
fi

local set shopt; ble/base/.adjust-bash-options set shopt
local opt_flags=
local -a opt_args=()
ble/builtin/exit/.read-arguments "$@"
if [[ $opt_flags == *[EH]* ]]; then
[[ $opt_flags == *H* ]] && builtin exit --help
ble/base/.restore-bash-options set shopt
return 2
fi
((${#opt_args[@]})) || ble/array#push opt_args "$ext"
Expand All @@ -5324,12 +5326,15 @@ function ble/builtin/exit {
ble/builtin/read -ep "\e[38;5;12m[ble: There are $cancel_reason]\e[m Leave the shell anyway? [yes/No] " ret
case $ret in
([yY]|[yY][eE][sS]) break ;;
([nN]|[nN][oO]|'') return 0 ;;
([nN]|[nN][oO]|'')
ble/base/.restore-bash-options set shopt
return 0 ;;
esac
done
fi

ble/util/print "${_ble_term_setaf[12]}[ble: exit]$_ble_term_sgr0" >&2
ble/base/.restore-bash-options set shopt
builtin exit "${opt_args[@]}" &>/dev/null
builtin exit "${opt_args[@]}" &>/dev/null
return 1 # exit できなかった場合は 1 らしい
Expand Down Expand Up @@ -8334,6 +8339,9 @@ function ble/builtin/read {
return "$?"
fi

local _ble_local_set _ble_local_shopt
ble/base/.adjust-bash-options _ble_local_set _ble_local_shopt

# used by core-complete to cancel progcomp
[[ $_ble_builtin_read_hook ]] &&
builtin eval -- "$_ble_builtin_read_hook"
Expand All @@ -8342,8 +8350,9 @@ function ble/builtin/read {
[[ ! $_ble_attached || $_ble_edit_exec_inside_userspace ]] && ble-edit/exec/save-BASH_REMATCH
ble/builtin/read/.impl "$@"; local __ble_ext=$?
[[ ! $_ble_attached || $_ble_edit_exec_inside_userspace ]] && ble-edit/exec/restore-BASH_REMATCH
[[ $__ble_command ]] || return "$__ble_ext"

ble/base/.restore-bash-options _ble_local_set _ble_local_shopt
[[ $__ble_command ]] || return "$__ble_ext"
# 局所変数により被覆されないように外側で評価
builtin eval -- "$__ble_command"
}
Expand Down
12 changes: 10 additions & 2 deletions src/history.sh
Expand Up @@ -1426,6 +1426,7 @@ function ble/builtin/history/option:s {
_ble_builtin_history_prevmax=$max
}
function ble/builtin/history {
local set shopt; ble/base/.adjust-bash-options set shopt
local opt_d= flag_error=
local opt_c= opt_p= opt_s=
local opt_a= flags=
Expand Down Expand Up @@ -1472,12 +1473,15 @@ function ble/builtin/history {
done
if [[ $flag_error ]]; then
builtin history --usage 2>&1 1>/dev/null | ble/bin/grep ^history >&2
ble/base/.restore-bash-options set shopt
return 2
fi

if [[ $flags == *h* ]]; then
builtin history --help
return "$?"
local ext=$?
ble/base/.restore-bash-options set shopt
return "$ext"
fi

[[ ! $_ble_attached || $_ble_edit_exec_inside_userspace ]] &&
Expand All @@ -1500,7 +1504,10 @@ function ble/builtin/history {
ble/builtin/history/option:"$opt_a" "$1"
flag_processed=1
fi
[[ $flag_processed ]] && return 0
if [[ $flag_processed ]]; then
ble/base/.restore-bash-options set shopt
return 0
fi

# -p
if [[ $opt_p ]]; then
Expand All @@ -1511,6 +1518,7 @@ function ble/builtin/history {

[[ ! $_ble_attached || $_ble_edit_exec_inside_userspace ]] &&
ble-edit/exec/restore-BASH_REMATCH
ble/base/.restore-bash-options set shopt
return "$ext"
}
function history { ble/builtin/history "$@"; }
Expand Down
14 changes: 11 additions & 3 deletions src/util.sh
Expand Up @@ -1978,14 +1978,17 @@ function ble/builtin/trap/invoke {
builtin eval -- "${_ble_builtin_trap_handlers[ret]}" 2>&3
} 3>&2 2>/dev/null # set -x 対策 #D0930
function ble/builtin/trap {
local set shopt; ble/base/.adjust-bash-options set shopt
local flags command sigspecs
ble/builtin/trap/.read-arguments "$@"

if [[ $flags == *h* ]]; then
builtin trap --help
ble/base/.restore-bash-options set shopt
return 2
elif [[ $flags == *E* ]]; then
builtin trap --usage 2>&1 1>/dev/null | ble/bin/grep ^trap >&2
ble/base/.restore-bash-options set shopt
return 2
elif [[ $flags == *l* ]]; then
builtin trap -l
Expand Down Expand Up @@ -2044,6 +2047,7 @@ function ble/builtin/trap {

[[ ! $_ble_attached || $_ble_edit_exec_inside_userspace ]] &&
ble-edit/exec/restore-BASH_REMATCH
ble/base/.restore-bash-options set shopt
return 0
}
function trap { ble/builtin/trap "$@"; }
Expand Down Expand Up @@ -3046,7 +3050,7 @@ else
# Note: bash-4.3 以下では BASH_SUBSHELL はパイプやプロセス置換で増えないの
# で信頼性が低いらしい。唯、関数内で実行している限りは大丈夫なのかもしれ
# ない。
((BASH_SUBSHELL)) && return 0
((BASH_SUBSHELL==0)) || return 0
local BASHPID; ble/util/getpid
[[ $$ != $BASHPID ]]
}
Expand Down Expand Up @@ -3725,6 +3729,7 @@ if ((_ble_bash>=40400)) && ble/util/msleep/.check-builtin-sleep; then
}

function ble/builtin/sleep {
local set shopt; ble/base/.adjust-bash-options set shopt
local frac_scale=100000000000000
local a=0 b=0 flags=
if (($#==0)); then
Expand Down Expand Up @@ -3761,14 +3766,17 @@ if ((_ble_bash>=40400)) && ble/util/msleep/.check-builtin-sleep; then
ble/util/print "sleep (ble) $BLE_VERSION"
fi
if [[ $flags == *E* ]]; then
return 2
ble/util/setexit 2
elif [[ $flags == *[vh]* ]]; then
return 0
ble/util/setexit 0
else
b=00000000000000$b
b=${b:${#b}-14}
builtin sleep "$a.$b"
fi
local ext=$?
ble/base/.restore-bash-options set shopt 1
return "$ext"
}
function sleep { ble/builtin/sleep "$@"; }
elif [[ -f $_ble_base/lib/init-msleep.sh ]] &&
Expand Down

0 comments on commit 001c595

Please sign in to comment.