Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/bash/git/lib_git.sh
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ git_get_current_branch() {
log_error "Usage: git_get_current_branch <directory> <result_variable_name>"
return 1
fi
if ! __is_valid_variable_name__ "$result_var_name"; then
log_error "git_get_current_branch: result variable name must be a valid Bash variable name."
return 1
fi

printf -v "$result_var_name" '%s' ""

Expand Down
12 changes: 12 additions & 0 deletions lib/bash/git/tests/lib_git.bats
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ setup() {
[[ "$output" != *"Usage: get_git_branch"* ]]
}

@test "git_get_current_branch rejects invalid result variable names" {
local repo="$TEST_TMPDIR/repo"

init_git_repo "$repo"

bats_run git_get_current_branch "$repo" "bad-name"

[ "$status" -eq 1 ]
[[ "$output" == *"git_get_current_branch: result variable name must be a valid Bash variable name"* ]]
[[ "$output" != *"invalid variable name"* ]]
}

@test "git_update_repo skips dirty repositories when no dirty path is allowed" {
local repo="$TEST_TMPDIR/repo"

Expand Down
18 changes: 16 additions & 2 deletions lib/bash/std/lib_std.sh
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,11 @@ safe_truncate() {

####################################################### ASSERTIONS ####################################################

__is_valid_variable_name__() {
local var_name="${1-}" var_name_re='^[A-Za-z_][A-Za-z0-9_]*$'
[[ "$var_name" =~ $var_name_re ]]
}

#
# assert_not_null - Checks that one or more variables are not empty.
#
Expand All @@ -861,13 +866,13 @@ safe_truncate() {
# $@: One or more variable names to check.
#
assert_not_null() {
local unset_vars=() var_name var_name_re='^[A-Za-z_][A-Za-z0-9_]*$'
local unset_vars=() var_name
if (($# == 0)); then
fatal_error "assert_not_null: No variable names provided for validation."
fi

for var_name in "$@"; do
if ! [[ "$var_name" =~ $var_name_re ]]; then
if ! __is_valid_variable_name__ "$var_name"; then
fatal_error "assert_not_null expects variable names, not values; one or more arguments are not valid Bash variable names."
fi
# Use indirection to get the value of the variable whose name is stored in var_name.
Expand All @@ -892,6 +897,9 @@ assert_integer() {
local var_name int_re='^[-+]?[0-9]+$'
(($# == 0)) && fatal_error "assert_integer: No variable names provided."
for var_name in "$@"; do
if ! __is_valid_variable_name__ "$var_name"; then
fatal_error "assert_integer expects variable names, not values; one or more arguments are not valid Bash variable names."
fi
local value="${!var_name-}"
! [[ "$value" =~ $int_re ]] && fatal_error "Variable '$var_name' with value '$value' is not a valid integer."
done
Expand All @@ -909,6 +917,9 @@ assert_integer() {
assert_integer_range() {
local var_name="${1-}" min="${2-}" max="${3-}"
(($# != 3)) && fatal_error "assert_integer_range: Expected 3 arguments, got $#."
if ! __is_valid_variable_name__ "$var_name"; then
fatal_error "assert_integer_range expects a variable name as its first argument."
fi
local value="${!var_name-}"
assert_integer "$var_name" min max
((value < min || value > max)) && fatal_error "Variable '$var_name' ($value) is out of range [$min, $max]."
Expand Down Expand Up @@ -1134,6 +1145,9 @@ safe_unalias() {
get_my_source_dir() {
local result_name="${1-}"
[[ -n "$result_name" ]] || fatal_error "get_my_source_dir: No result variable name provided."
if ! __is_valid_variable_name__ "$result_name"; then
fatal_error "get_my_source_dir: result variable name must be a valid Bash variable name."
fi
local source_dir
# Reference: https://stackoverflow.com/a/246128/6862601
source_dir="$(cd "$(dirname "${BASH_SOURCE[1]}")" >/dev/null 2>&1 && pwd -P)" ||
Expand Down
54 changes: 54 additions & 0 deletions lib/bash/std/tests/lib_std.bats
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,24 @@ EOF
[[ "$output" == *"is not a valid integer"* ]]
}

@test "assert_integer rejects invalid variable names without echoing values" {
local script="$TEST_TMPDIR/assert-integer-name.sh"

create_script "$script" <<EOF
#!/usr/bin/env bash
source "$STDLIB_PATH"
secret="not-a-var-name"
assert_integer "\$secret"
EOF

bats_run bash "$script"

[ "$status" -eq 1 ]
[[ "$output" == *"assert_integer expects variable names"* ]]
[[ "$output" != *"not-a-var-name"* ]]
[[ "$output" != *"invalid variable name"* ]]
}

@test "assert_integer_range enforces range bounds" {
local count=5
local script="$TEST_TMPDIR/assert-range.sh"
Expand All @@ -1011,6 +1029,24 @@ EOF
[[ "$output" == *"is out of range [1, 10]"* ]]
}

@test "assert_integer_range rejects invalid variable names without echoing values" {
local script="$TEST_TMPDIR/assert-range-name.sh"

create_script "$script" <<EOF
#!/usr/bin/env bash
source "$STDLIB_PATH"
secret="bad-range-name"
assert_integer_range "\$secret" 1 10
EOF

bats_run bash "$script"

[ "$status" -eq 1 ]
[[ "$output" == *"assert_integer_range expects a variable name"* ]]
[[ "$output" != *"bad-range-name"* ]]
[[ "$output" != *"invalid variable name"* ]]
}

@test "assert_arg_count accepts exact and ranged matches" {
assert_arg_count 2 2
assert_arg_count 2 1 3
Expand Down Expand Up @@ -1195,6 +1231,24 @@ EOF
[[ "$output" == *"dir=$expected_dir"* ]]
}

@test "get_my_source_dir rejects invalid result variable names" {
local script="$TEST_TMPDIR/get-source-dir-invalid.sh"

create_script "$script" <<EOF
#!/usr/bin/env bash
source "$STDLIB_PATH"
get_my_source_dir "bad-name"
echo "after"
EOF

bats_run bash "$script"

[ "$status" -eq 1 ]
[[ "$output" == *"get_my_source_dir: result variable name must be a valid Bash variable name"* ]]
[[ "$output" != *"invalid variable name"* ]]
[[ "$output" != *"after"* ]]
}

@test "ask_yes_no accepts yes input" {
local script="$TEST_TMPDIR/ask-yes.sh"

Expand Down
Loading