From 02c13b7921ac86e8ec4742ce82c9c883f678c3da Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sun, 25 Jul 2021 20:02:48 -0700 Subject: [PATCH 01/12] lib/log: handle undefined variables If the user hasn't defined BASH_IT_LOG_LEVEL, then the integer comparison fails. Handle it by defaulting to '1'. If lib/log is loaded improperly, the BASH_IT_LOG_PREFIX may be undefined. Unlikely, but no harm in handling it too. Likewise, if no theme is loaded, then $echo_green, $echo_normal, et al are not defined. --- lib/log.bash | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) mode change 100644 => 100755 lib/log.bash diff --git a/lib/log.bash b/lib/log.bash old mode 100644 new mode 100755 index 105c9064a1..401fa3fa57 --- a/lib/log.bash +++ b/lib/log.bash @@ -22,8 +22,8 @@ function _log_general() param '3: message to log' group 'log' - message=$2${BASH_IT_LOG_PREFIX}$3 - _has_colors && echo -e "$1${message}${echo_normal}" || echo -e "${message}" + message=$2${BASH_IT_LOG_PREFIX:-default: }$3 + _has_colors && echo -e "$1${message}${echo_normal:-}" || echo -e "${message}" } function _log_debug() @@ -33,8 +33,8 @@ function _log_debug() example '$ _log_debug "Loading plugin git..."' group 'log' - [[ "$BASH_IT_LOG_LEVEL" -ge $BASH_IT_LOG_LEVEL_ALL ]] || return 0 - _log_general "${echo_green}" "DEBUG: " "$1" + [[ "${BASH_IT_LOG_LEVEL:-1}" -ge $BASH_IT_LOG_LEVEL_ALL ]] || return 0 + _log_general "${echo_green:-}" "DEBUG: " "$1" } function _log_warning() @@ -44,7 +44,7 @@ function _log_warning() example '$ _log_warning "git binary not found, disabling git plugin..."' group 'log' - [[ "$BASH_IT_LOG_LEVEL" -ge $BASH_IT_LOG_LEVEL_WARNING ]] || return 0 + [[ "${BASH_IT_LOG_LEVEL:-1}" -ge $BASH_IT_LOG_LEVEL_WARNING ]] || return 0 _log_general "${echo_yellow}" " WARN: " "$1" } @@ -55,6 +55,6 @@ function _log_error() example '$ _log_error "Failed to load git plugin..."' group 'log' - [[ "$BASH_IT_LOG_LEVEL" -ge $BASH_IT_LOG_LEVEL_ERROR ]] || return 0 - _log_general "${echo_red}" "ERROR: " "$1" + [[ "${BASH_IT_LOG_LEVEL:-1}" -ge $BASH_IT_LOG_LEVEL_ERROR ]] || return 0 + _log_general "${echo_red:-}" "ERROR: " "$1" } From bcd67db4ab7a6514ca58c24db9e53a4c1b60da8b Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sun, 25 Jul 2021 21:09:51 -0700 Subject: [PATCH 02/12] lib/preview: handle unbound parameter If $BASH_PREVIEW is unset, treat it as blank. --- lib/preview.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/preview.bash b/lib/preview.bash index d0fb0623ba..418839cd2d 100644 --- a/lib/preview.bash +++ b/lib/preview.bash @@ -1,4 +1,4 @@ -if [[ $BASH_PREVIEW ]]; +if [[ "${BASH_PREVIEW:-}" ]]; then unset BASH_PREVIEW #Prevent infinite looping echo " From 5001995e92d78cc29c580d9f7e1ad5dad2dd0071 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sun, 25 Jul 2021 21:25:45 -0700 Subject: [PATCH 03/12] Deal with unabound BASH_IT_RELOAD_LEGACY --- bash_it.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bash_it.sh b/bash_it.sh index cb50420291..1289399089 100755 --- a/bash_it.sh +++ b/bash_it.sh @@ -144,7 +144,7 @@ if [ -e "$HOME/.jekyllconfig" ]; then fi # BASH_IT_RELOAD_LEGACY is set. -if ! command -v reload &> /dev/null && [ -n "$BASH_IT_RELOAD_LEGACY" ]; then +if ! command -v reload &> /dev/null && [ -n "${BASH_IT_RELOAD_LEGACY:-}" ]; then case $OSTYPE in darwin*) alias reload='source ~/.bash_profile' From f71fa5be2cb5ce177e593a6f62cd584f0364d7dd Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sun, 25 Jul 2021 21:30:28 -0700 Subject: [PATCH 04/12] Handle unbound variables Handle BASH_IT, BASH_IT_OLD_BASH_SETUP, BASH_IT_THEME, BASH_THEME, and PROMPT. --- bash_it.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bash_it.sh b/bash_it.sh index 1289399089..b8d3aeef2b 100755 --- a/bash_it.sh +++ b/bash_it.sh @@ -3,7 +3,7 @@ BASH_IT_LOG_PREFIX="core: main: " # Only set $BASH_IT if it's not already set -if [ -z "$BASH_IT" ]; then +if [ -z "${BASH_IT:-}" ]; then # Setting $BASH to maintain backwards compatibility export BASH_IT=$BASH BASH="$(bash -c 'echo $BASH')" @@ -20,12 +20,12 @@ source "${BASH_IT}"/vendor/github.com/erichs/composure/composure.sh source "${BASH_IT}/lib/log.bash" # We can only log it now -[ -z "$BASH_IT_OLD_BASH_SETUP" ] || _log_warning "BASH_IT variable not initialized, please upgrade your bash-it version and reinstall it!" +[ -z "${BASH_IT_OLD_BASH_SETUP:-}" ] || _log_warning "BASH_IT variable not initialized, please upgrade your bash-it version and reinstall it!" # For backwards compatibility, look in old BASH_THEME location -if [ -z "$BASH_IT_THEME" ]; then +if [ -z "${BASH_IT_THEME:-}" ]; then _log_warning "BASH_IT_THEME variable not initialized, please upgrade your bash-it version and reinstall it!" - export BASH_IT_THEME="$BASH_THEME" + export BASH_IT_THEME="${BASH_THEME:-}" unset BASH_THEME fi @@ -122,7 +122,7 @@ for _bash_it_config_file in $CUSTOM; do done unset _bash_it_config_file -if [[ $PROMPT ]]; then +if [[ "${PROMPT:-}" ]]; then export PS1="\[""$PROMPT""\]" fi From 233fcc30910c84ddd490ea41644f881c1b8fdba1 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sun, 25 Jul 2021 21:46:18 -0700 Subject: [PATCH 05/12] lib/log: handle undefined variable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Expressly deal with if $echo_yellow hasn’t been defined --- lib/log.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/log.bash b/lib/log.bash index 401fa3fa57..32626d7666 100755 --- a/lib/log.bash +++ b/lib/log.bash @@ -45,7 +45,7 @@ function _log_warning() group 'log' [[ "${BASH_IT_LOG_LEVEL:-1}" -ge $BASH_IT_LOG_LEVEL_WARNING ]] || return 0 - _log_general "${echo_yellow}" " WARN: " "$1" + _log_general "${echo_yellow:-}" " WARN: " "$1" } function _log_error() From 0eff44930c3aa9b01d408ff06374a671c8266263 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sun, 25 Jul 2021 21:59:40 -0700 Subject: [PATCH 06/12] lib/utilities: handle unbound parameter Expressly handle unbound $BASH_IT_GREP when testing for value --- lib/utilities.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) mode change 100644 => 100755 lib/utilities.bash diff --git a/lib/utilities.bash b/lib/utilities.bash old mode 100644 new mode 100755 index db63956d74..b6322a1d7c --- a/lib/utilities.bash +++ b/lib/utilities.bash @@ -58,7 +58,7 @@ _bash-it-array-dedup() { # Outputs a full path of the grep found on the filesystem _bash-it-grep() { - if [[ -z "${BASH_IT_GREP}" ]] ; then + if [[ -z "${BASH_IT_GREP:-}" ]] ; then export BASH_IT_GREP="$(which egrep || which grep || '/usr/bin/grep')" fi printf "%s " "${BASH_IT_GREP}" From 8fb75cf0e396952e152424cb5fdadb2d1ae1e1bc Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sun, 25 Jul 2021 23:01:02 -0700 Subject: [PATCH 07/12] lib/helpers: handle unbound parameter Expressly handle $BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE being not-set as being blank. --- lib/helpers.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/helpers.bash b/lib/helpers.bash index a3468a6364..81d01c7243 100755 --- a/lib/helpers.bash +++ b/lib/helpers.bash @@ -156,7 +156,7 @@ bash-it () $func $arg done - if [ -n "$BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE" ]; then + if [ -n "${BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE:-}" ]; then _bash-it-reload fi else From bd9f4015a7d6b09d2848efaf393637836e7b48b7 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sun, 25 Jul 2021 23:18:56 -0700 Subject: [PATCH 08/12] lib/helpers: handle unset parameter Expressly handle $BASH_IT_REMOTE as blank when variable is not set. --- lib/helpers.bash | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/helpers.bash b/lib/helpers.bash index 81d01c7243..a528c14c0f 100755 --- a/lib/helpers.bash +++ b/lib/helpers.bash @@ -354,8 +354,8 @@ _bash-it-migrate() { disable_func="_disable-$single_type" enable_func="_enable-$single_type" - $disable_func $component_name - $enable_func $component_name + $disable_func "$component_name" + $enable_func "$component_name" done done @@ -375,7 +375,7 @@ _bash-it-version() { cd "${BASH_IT}" || return - if [ -z $BASH_IT_REMOTE ]; then + if [ -z "${BASH_IT_REMOTE:-}" ]; then BASH_IT_REMOTE="origin" fi From cd65e0925fe4338caebb171b8c2da13322f3e2de Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sat, 14 Aug 2021 20:19:12 -0700 Subject: [PATCH 09/12] plugins/base: unbound BASH_IT_LEGACY_PASS Expressly handle as blank when $BASH_IT_LEGACY_PASS is not set. --- plugins/available/base.plugin.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/available/base.plugin.bash b/plugins/available/base.plugin.bash index 922fd9ec75..8499a2dfe0 100755 --- a/plugins/available/base.plugin.bash +++ b/plugins/available/base.plugin.bash @@ -70,7 +70,7 @@ function passgen () # Create alias pass to passgen when pass isn't installed or # BASH_IT_LEGACY_PASS is true. -if ! command -v pass &>/dev/null || [[ "$BASH_IT_LEGACY_PASS" = true ]] +if ! command -v pass &>/dev/null || [[ "${BASH_IT_LEGACY_PASS:-}" = true ]] then alias pass=passgen fi From 0c412442f5108805a1f5e3cd602b1458b1f16556 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Wed, 11 Aug 2021 18:11:44 -0700 Subject: [PATCH 10/12] completion/system: give up hope Give up and accept defeat that bash-completion can't reasonably be audited for unbound parameters. Wrap invocation with disabling strictness, and restore after if it was enabled. --- completion/available/system.completion.bash | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/completion/available/system.completion.bash b/completion/available/system.completion.bash index 142e08802b..c4230a1c09 100755 --- a/completion/available/system.completion.bash +++ b/completion/available/system.completion.bash @@ -3,6 +3,14 @@ # Loads the system's Bash completion modules. # If Homebrew is installed (OS X), it's Bash completion modules are loaded. +if shopt -qo nounset +then # Bash-completion is too large and complex to expect to handle unbound variables throughout the whole codebase. + BASH_IT_RESTORE_NOUNSET=true + shopt -uo nounset +else + BASH_IT_RESTORE_NOUNSET=false +fi + if [[ -r "${BASH_COMPLETION:-}" ]] ; then source "${BASH_COMPLETION}" elif [[ -r /etc/bash_completion ]] ; then @@ -22,3 +30,9 @@ then source "$BASH_IT_HOMEBREW_PREFIX"/etc/profile.d/bash_completion.sh fi fi + +if $BASH_IT_RESTORE_NOUNSET +then + shopt -so nounset +fi +unset BASH_IT_RESTORE_NOUNSET From 3654198deb8e56f915515dbba96f1422f6d52016 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sun, 1 Aug 2021 21:45:35 -0700 Subject: [PATCH 11/12] Tests for 'default' prefix --- test/lib/log.bats | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/lib/log.bats b/test/lib/log.bats index 015068e68b..f4a04f0e1a 100644 --- a/test/lib/log.bats +++ b/test/lib/log.bats @@ -11,19 +11,19 @@ load ../../lib/log @test "lib log: basic debug logging with BASH_IT_LOG_LEVEL_ALL" { BASH_IT_LOG_LEVEL=$BASH_IT_LOG_LEVEL_ALL run _log_debug "test test test" - assert_output "DEBUG: test test test" + assert_output "DEBUG: default: test test test" } @test "lib log: basic warning logging with BASH_IT_LOG_LEVEL_ALL" { BASH_IT_LOG_LEVEL=$BASH_IT_LOG_LEVEL_ALL run _log_warning "test test test" - assert_output " WARN: test test test" + assert_output " WARN: default: test test test" } @test "lib log: basic error logging with BASH_IT_LOG_LEVEL_ALL" { BASH_IT_LOG_LEVEL=$BASH_IT_LOG_LEVEL_ALL run _log_error "test test test" - assert_output "ERROR: test test test" + assert_output "ERROR: default: test test test" } @test "lib log: basic debug logging with BASH_IT_LOG_LEVEL_WARNING" { @@ -35,13 +35,13 @@ load ../../lib/log @test "lib log: basic warning logging with BASH_IT_LOG_LEVEL_WARNING" { BASH_IT_LOG_LEVEL=$BASH_IT_LOG_LEVEL_WARNING run _log_warning "test test test" - assert_output " WARN: test test test" + assert_output " WARN: default: test test test" } @test "lib log: basic error logging with BASH_IT_LOG_LEVEL_WARNING" { BASH_IT_LOG_LEVEL=$BASH_IT_LOG_LEVEL_WARNING run _log_error "test test test" - assert_output "ERROR: test test test" + assert_output "ERROR: default: test test test" } @@ -60,7 +60,7 @@ load ../../lib/log @test "lib log: basic error logging with BASH_IT_LOG_LEVEL_ERROR" { BASH_IT_LOG_LEVEL=$BASH_IT_LOG_LEVEL_ERROR run _log_error "test test test" - assert_output "ERROR: test test test" + assert_output "ERROR: default: test test test" } @test "lib log: basic debug silent logging" { From 85408743fa41a0331fb6fed57e0539a0a43117df Mon Sep 17 00:00:00 2001 From: John D Pell Date: Tue, 3 Aug 2021 18:18:14 -0400 Subject: [PATCH 12/12] lib/log: default to no logging at all Set the default when BASH_IT_LOG_LEVEL is unbound to log level none: no warnings or errors are reported at all. --- lib/log.bash | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/log.bash b/lib/log.bash index 32626d7666..6bc53a1239 100755 --- a/lib/log.bash +++ b/lib/log.bash @@ -33,7 +33,7 @@ function _log_debug() example '$ _log_debug "Loading plugin git..."' group 'log' - [[ "${BASH_IT_LOG_LEVEL:-1}" -ge $BASH_IT_LOG_LEVEL_ALL ]] || return 0 + [[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_ALL ]] || return 0 _log_general "${echo_green:-}" "DEBUG: " "$1" } @@ -44,7 +44,7 @@ function _log_warning() example '$ _log_warning "git binary not found, disabling git plugin..."' group 'log' - [[ "${BASH_IT_LOG_LEVEL:-1}" -ge $BASH_IT_LOG_LEVEL_WARNING ]] || return 0 + [[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_WARNING ]] || return 0 _log_general "${echo_yellow:-}" " WARN: " "$1" } @@ -55,6 +55,6 @@ function _log_error() example '$ _log_error "Failed to load git plugin..."' group 'log' - [[ "${BASH_IT_LOG_LEVEL:-1}" -ge $BASH_IT_LOG_LEVEL_ERROR ]] || return 0 + [[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_ERROR ]] || return 0 _log_general "${echo_red:-}" "ERROR: " "$1" }