From 001c595881cfad4cc59adebf62eca8d208f8ab89 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Sat, 22 Jan 2022 13:29:31 +0900 Subject: [PATCH] global (ble/builtin/*): work around "set -eu" in NixOS initialization --- ble.pp | 1 - docs/ChangeLog.md | 1 + note.txt | 23 +++++++++++++++++++++++ src/decode.sh | 10 ++-------- src/edit.sh | 13 +++++++++++-- src/history.sh | 12 ++++++++++-- src/util.sh | 14 +++++++++++--- 7 files changed, 58 insertions(+), 16 deletions(-) diff --git a/ble.pp b/ble.pp index 92aa32d1..9690627f 100644 --- a/ble.pp +++ b/ble.pp @@ -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 diff --git a/docs/ChangeLog.md b/docs/ChangeLog.md index ab2ee0d9..00ed7680 100644 --- a/docs/ChangeLog.md +++ b/docs/ChangeLog.md @@ -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 diff --git a/note.txt b/note.txt index 2d469ed7..36e6a51a 100644 --- a/note.txt +++ b/note.txt @@ -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 diff --git a/src/decode.sh b/src/decode.sh index fa392b1c..b5e3dcab 100644 --- a/src/decode.sh +++ b/src/decode.sh @@ -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 @@ -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 "$@"; } diff --git a/src/edit.sh b/src/edit.sh index 3aa84c5c..169b1ea6 100644 --- a/src/edit.sh +++ b/src/edit.sh @@ -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" @@ -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 らしい @@ -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" @@ -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" } diff --git a/src/history.sh b/src/history.sh index 5ae78039..2d63f5a8 100644 --- a/src/history.sh +++ b/src/history.sh @@ -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= @@ -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 ]] && @@ -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 @@ -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 "$@"; } diff --git a/src/util.sh b/src/util.sh index dea9f90b..c18c6c82 100644 --- a/src/util.sh +++ b/src/util.sh @@ -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 @@ -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 "$@"; } @@ -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 ]] } @@ -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 @@ -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 ]] &&