Workaround for legacy Bash array expansion
Some older versions of Bash have a bug that treats '${array[@]}' as
an undefined variable access if the array is empty. Under normal
circumstances, it results in an empty string. However, this crashes
with 'set -u'. Since we do run with this setting for safety, and
Busytown seems to be using an affected version of Bash, replace uses
of this construct with '${array[@]+"${array[@]}"}'. Uses where the
array cannot be empty are left unchanged.
Analysis of the bug:
https://gist.github.com/dimo414/2fb052d230654cc0c25e9e41a9651ebe
Change-Id: I1bbfbee3ac0266db531d5d4d693def8292bf7358
diff --git a/aarch64/run_qemu.sh b/aarch64/run_qemu.sh
index 75002b7..226f288 100755
--- a/aarch64/run_qemu.sh
+++ b/aarch64/run_qemu.sh
@@ -100,7 +100,9 @@
CMD+=("${QEMU}")
APPEND+=(rootwait root=/dev/vda)
-for MOUNT in "${EXTRA_RO_MOUNTS[@]}"; do
+# Note: Due to a bug in older versions of Bash, use '${array[@]+"${array[@]}"}'
+# to expand potentially empty arrays. '${array[@]}' is treated as undefined.
+for MOUNT in ${EXTRA_RO_MOUNTS[@]+"${EXTRA_RO_MOUNTS[@]}"}; do
EXTRA_ARGS+=(-drive "file=${MOUNT},readonly,if=virtio,format=raw")
done
@@ -113,6 +115,8 @@
set -x
fi
+# Note: Due to a bug in older versions of Bash, use '${array[@]+"${array[@]}"}'
+# to expand potentially empty arrays. '${array[@]}' is treated as undefined.
exec "${CMD[@]}" \
-M virt \
-machine virtualization=true -machine virt,gic-version=${GIC} \
@@ -123,4 +127,4 @@
-device virtio-rng-pci,rng=rng0 \
-nographic -nodefaults -serial stdio \
-append "${APPEND[*]}" \
- "${EXTRA_ARGS[@]}" \
+ ${EXTRA_ARGS[@]+"${EXTRA_ARGS[@]}"}
diff --git a/kvm-unit-tests/gen_makefile.sh b/kvm-unit-tests/gen_makefile.sh
index e484ebb..bbd5b32 100755
--- a/kvm-unit-tests/gen_makefile.sh
+++ b/kvm-unit-tests/gen_makefile.sh
@@ -98,6 +98,8 @@
extra_args+=(-G)
fi
+ # Note: Due to a bug in older versions of Bash, use '${array[@]+"${array[@]}"}'
+ # to expand potentially empty arrays. '${array[@]}' is treated as undefined.
cat <<EOF
.PHONY: ${target}
${target}: \$(${VAR_KERNEL_IMAGE}) ${binary}
@@ -106,7 +108,7 @@
-k \$(${VAR_KERNEL_IMAGE}) \
-d "${target}" \
-o \$(${VAR_LOG_DIR})/${target}.log \
- ${extra_args[@]} \
+ ${extra_args[@]+"${extra_args[@]}"} \
"${binary}"
EOF
}
@@ -158,5 +160,7 @@
# Emit a target which prints all test targets from the generated Makefile.
ROOT="${GLOBAL_TEST_LIST_TARGET}"
TARGET="$(list_target_name "${ROOT}")"
-test_list_target "${TARGET}" "${TARGET_LIST[@]}"
+# Note: Due to a bug in older versions of Bash, use '${array[@]+"${array[@]}"}'
+# to expand potentially empty arrays. '${array[@]}' is treated as undefined.
+test_list_target "${TARGET}" ${TARGET_LIST[@]+"${TARGET_LIST[@]}"}
dependency_target "${TARGET}" "${ROOT}"
diff --git a/kvm-unit-tests/run_test.sh b/kvm-unit-tests/run_test.sh
index 8d90fe3..b38d5cc 100755
--- a/kvm-unit-tests/run_test.sh
+++ b/kvm-unit-tests/run_test.sh
@@ -89,19 +89,6 @@
TEST_PATH="$1"
shift 1
-# If there are arguments after "--", pass them to the underlying run_qemu.sh.
-RUN_QEMU_ARGS=()
-if [ $# -gt 0 ]; then
- if [ $1 = "--" ]; then
- shift 1
- RUN_QEMU_ARGS=("$@")
- else
- echo "Unrecognized options: $@" 1>&2
- usage 1>&2
- exit 1
- fi
-fi
-
# Default display name is the basename from TEST_PATH.
if [ -z "${DISPLAY_NAME}" ]; then
DISPLAY_NAME="$(basename "${TEST_PATH}")"
@@ -129,8 +116,19 @@
CMD+=(-G)
fi
-# Extra arguments passed directly to the underlying script.
-CMD+=("${RUN_QEMU_ARGS[@]}")
+# If there are arguments after "--", pass them to the underlying run_qemu.sh.
+if [ $# -gt 0 ]; then
+ if [ $1 = "--" ]; then
+ shift 1
+ # Note: Due to a bug in older versions of Bash, use'${array[@]+"${array[@]}"}'
+ # to expand potentially empty arrays. '${array[@]}' is treated as undefined.
+ CMD+=(${@+"$@"})
+ else
+ echo "Unrecognized options: $@" 1>&2
+ usage 1>&2
+ exit 1
+ fi
+fi
# Disable exiting on failure.
set +eo pipefail