]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1653] friendly errors, not unbound variable
authorAndrei Pavel <andrei@isc.org>
Sun, 7 Feb 2021 09:30:55 +0000 (11:30 +0200)
committerAndrei Pavel <andrei@isc.org>
Tue, 22 Jun 2021 08:48:26 +0000 (11:48 +0300)
src/bin/admin/kea-admin.in
src/bin/keactrl/keactrl.in
src/lib/asiolink/tests/process_spawn_app.sh.in
src/lib/testutils/dhcp_test_lib.sh.in
tools/mk_cfgrpt.sh

index 2d1939130f186e4aee5196c420a971bdf3450395..78c98a02d258c0d5d662e21f6c674ee89ed7a5d9 100644 (file)
@@ -113,9 +113,9 @@ log_info() {
 # is to determine whether the kea-admin command belongs to the list of
 # supported commands.
 is_in_list() {
-    local member=${1}  # Value to be checked
-    local list="${2}"  # Comma separated list of items
-    _inlist=0          # Return value: 0 if not in list, 1 otherwise.
+    local member="${1-}"  # Value to be checked
+    local list="${2-}"    # Comma separated list of items
+    _inlist=0             # Return value: 0 if not in list, 1 otherwise.
     if [ -z "${member}" ]; then
         log_error "missing member (need to specify a string as first param)"
     fi
@@ -698,7 +698,7 @@ fi
 shift
 
 # Second, check what's the backend
-backend=${1}
+backend=${1-}
 if [ -z "${backend}" ]; then
     log_error "missing backend"
     usage
@@ -719,7 +719,7 @@ do
         # Specify database host
         -h|--host)
             shift
-            db_host=${1}
+            db_host=${1-}
             if [ -z "${db_host}" ]; then
                 log_error "-h or --host requires a parameter"
                 usage
@@ -740,7 +740,7 @@ do
         # Specify database user
         -u|--user)
             shift
-            db_user=${1}
+            db_user=${1-}
             if [ -z "${db_user}" ]; then
                 log_error "-u or --user requires a parameter"
                 usage
@@ -768,7 +768,7 @@ do
         # Specify database name
         -n|--name)
             shift
-            db_name=${1}
+            db_name=${1-}
             if [ -z "${db_name}" ]; then
                 log_error "-n or --name requires a parameter"
                 usage
@@ -777,7 +777,7 @@ do
             ;;
         -d|--directory)
             shift
-            scripts_dir=${1}
+            scripts_dir=${1-}
             if [ -z "${scripts_dir}" ]; then
                 log_error "-d or --directory requires a parameter"
                 usage
@@ -805,7 +805,7 @@ do
         # specify output file, currently only used by lease dump
         -o|--output)
             shift
-            dump_file=${1}
+            dump_file=${1-}
             if [ -z "${dump_file}" ]; then
                 log_error "-o or --output requires a parameter"
                 usage
index d27699c927ee16809a9872f8cf7dafb7a24c3ec7..4a7d8b569ca92c20dffd3c79463f837f90566b08 100644 (file)
 # SC2154: ... is referenced but not assigned.
 # Reason: some variables are taken from keactrl.conf
 
+# Exit with error if commands exit with non-zero and if undefined variables are
+# used.
+set -eu
+
 VERSION="@PACKAGE_VERSION@"
 
 # Set the have_netconf flag to know if netconf is available.
@@ -52,9 +56,9 @@ log_info() {
 # is to determine whether the keactrl command belongs to the list of
 # supported commands.
 is_in_list() {
-    local member=${1}  # Value to be checked
-    local list="${2}"  # Comma separated list of items
-    _inlist=0          # Return value: 0 if not in list, 1 otherwise.
+    local member="${1-}"  # Value to be checked
+    local list="${2-}"    # Comma separated list of items
+    _inlist=0             # Return value: 0 if not in list, 1 otherwise.
     if [ -z "${member}" ]; then
         log_error "missing ${member}"
     fi
@@ -260,7 +264,7 @@ print_version() {
 # Check if the Kea configuration file location has been specified in the
 # keactrl configuration file. If not, it is a warning or a fatal error.
 check_kea_conf() {
-    local conf_file=${1} # Kea config file name.
+    local conf_file=${1-} # Kea config file name.
     if [ -z "${conf_file}" ]; then
         log_error "Configuration file for Kea not specified."
         exit 1
@@ -333,12 +337,12 @@ run_conditional() {
 # Note that when the configuration is applied this location may be
 # altered and only the handful of initial messages will be logged
 # to the default file.
-if [ -z "${KEA_LOGGER_DESTINATION}" ]; then
+if [ -z "${KEA_LOGGER_DESTINATION+x}" ]; then
     prefix="@prefix@"
     export KEA_LOGGER_DESTINATION="@localstatedir@/log/kea.log"
 fi
 
-command=${1}
+command=${1-}
 if [ -z "${command}" ]; then
     log_error "missing command"
     usage
@@ -365,14 +369,14 @@ keactrl_conf="@sysconfdir@/@PACKAGE@/keactrl.conf"
 servers="all"
 
 shift
-while [ -n "${1}" ]
+while test ${#} -gt 0
 do
     option=${1}
     case ${option} in
         # Override keactrl configuration file.
         -c|--ctrl-config)
             shift
-            keactrl_conf=${1}
+            keactrl_conf=${1-}
             if [ -z "${keactrl_conf}" ]; then
                 log_error "keactrl-config-file not specified"
                 usage
@@ -383,7 +387,7 @@ do
         # executed.
         -s|--server)
             shift
-            servers=$( printf "%s" "${1}" | tr "," "\n" )
+            servers=$(printf '%s' "${1-}" | tr ',' '\n')
             if [ -z "${servers}" ]; then
                 log_error "servers not specified"
                 usage
@@ -451,7 +455,7 @@ if ${have_netconf}; then
     # shellcheck disable=SC2154
     # SC2154: netconf_srv is referenced but not assigned.
     # reason: it is taken from keactrl.conf
-    if [ -z "${netconf_srv}" ]; then
+    if [ -z "${netconf_srv+x}" ]; then
         log_error "netconf_srv parameter not specified"
         exit 1
     fi
index e3d525c1152a73557fc6c6ab22cbfa1ebf5d58e2..eab73107116959926d2be113115eabe8c786562a 100644 (file)
@@ -39,7 +39,7 @@ do
             ;;
         -e)
             shift
-            exit_code=${1}
+            exit_code=${1-}
             ;;
         -s)
             shift
@@ -59,7 +59,11 @@ do
             exit 123
             ;;
     esac
-    shift
+    # We've shifted in the loop so we may have run out of parameters in the
+    # meantime. Check again.
+    if test "${#}" -gt 0; then
+      shift
+    fi
 done
 
 # The exit code of 32 is returned when no args specified or
index 2d1e36b92074b6f7e6c51a3e15f5f6bdfc01a8f6..280530113c3d05a76744e7c7c5b788387309df98 100644 (file)
@@ -48,9 +48,9 @@ fi
 
 # Prints error message.
 test_lib_error() {
-    local s=${1}            # Error message.
-    local no_new_line=${2}  # If specified, the message not terminated with
-                            # new line.
+    local s=${1-}            # Error message.
+    local no_new_line=${2-}  # If specified, the message not terminated with
+                             # new line.
     printf "ERROR/test_lib: %s" "${s}"
     if [ -z "${no_new_line}" ]; then
         printf '\n'
@@ -59,9 +59,9 @@ test_lib_error() {
 
 # Prints info message.
 test_lib_info() {
-    local s=${1}            # Info message.
-    local no_new_line=${2}  # If specified, the message is not terminated with
-                            # new line.
+    local s=${1-}            # Info message.
+    local no_new_line=${2-}  # If specified, the message is not terminated with
+                             # new line.
     printf "INFO/test_lib: %s" "${s}"
     if [ -z "${no_new_line}" ]; then
         printf '\n'
@@ -198,7 +198,7 @@ get_current_time() {
 
 # Begins a test by printing its name.
 test_start() {
-    TEST_NAME=${1}
+    TEST_NAME=${1-}
     if [ -z "${TEST_NAME}" ]; then
         test_lib_error "test_start requires test name as an argument"
         clean_exit 1
@@ -258,8 +258,8 @@ test_finish() {
 # Stores the configuration specified as a parameter in the configuration
 # file which name has been set in the ${CFG_FILE} variable.
 create_config() {
-    local cfg="${1}"  # Configuration string.
-    if [ -z "${CFG_FILE}" ]; then
+    local cfg="${1-}"  # Configuration string.
+    if [ -z "${CFG_FILE+x}" ]; then
         test_lib_error "create_config requires CFG_FILE variable be set"
         clean_exit 1
 
@@ -275,8 +275,8 @@ create_config() {
 # configuration file which name has been set in the ${DHCP4_CFG_FILE}
 # variable.
 create_dhcp4_config() {
-    local cfg="${1}"  # Configuration string.
-    if [ -z "${DHCP4_CFG_FILE}" ]; then
+    local cfg="${1-}"  # Configuration string.
+    if [ -z "${DHCP4_CFG_FILE+x}" ]; then
         test_lib_error "create_dhcp4_config requires DHCP4_CFG_FILE \
 variable be set"
         clean_exit 1
@@ -294,8 +294,8 @@ configuration"
 # configuration file which name has been set in the ${DHCP6_CFG_FILE}
 # variable.
 create_dhcp6_config() {
-    local cfg="${1}"  # Configuration string.
-    if [ -z "${DHCP6_CFG_FILE}" ]; then
+    local cfg="${1-}"  # Configuration string.
+    if [ -z "${DHCP6_CFG_FILE+x}" ]; then
         test_lib_error "create_dhcp6_config requires DHCP6_CFG_FILE \
 variable be set"
         clean_exit 1
@@ -313,8 +313,8 @@ configuration"
 # configuration file which name has been set in the ${D2_CFG_FILE}
 # variable.
 create_d2_config() {
-    local cfg="${1}"  # Configuration string.
-    if [ -z "${D2_CFG_FILE}" ]; then
+    local cfg="${1-}"  # Configuration string.
+    if [ -z "${D2_CFG_FILE+x}" ]; then
         test_lib_error "create_d2_config requires D2_CFG_FILE \
 variable be set"
         clean_exit 1
@@ -332,8 +332,8 @@ configuration"
 # configuration file which name has been set in the ${CA_CFG_FILE}
 # variable.
 create_ca_config() {
-    local cfg="${1}"  # Configuration string.
-    if [ -z "${CA_CFG_FILE}" ]; then
+    local cfg="${1-}"  # Configuration string.
+    if [ -z "${CA_CFG_FILE+x}" ]; then
         test_lib_error "create_ca_config requires CA_CFG_FILE \
 variable be set"
         clean_exit 1
@@ -351,8 +351,8 @@ configuration"
 # configuration file which name has been set in the ${NC_CFG_FILE}
 # variable.
 create_nc_config() {
-    local cfg="${1}"  # Configuration string.
-    if [ -z "${NC_CFG_FILE}" ]; then
+    local cfg="${1-}"  # Configuration string.
+    if [ -z "${NC_CFG_FILE+x}" ]; then
         test_lib_error "create_nc_config requires NC_CFG_FILE \
 variable be set"
         clean_exit 1
@@ -370,8 +370,8 @@ configuration"
 # configuration file which name has been set in the ${KEACTRL_CFG_FILE}
 # variable.
 create_keactrl_config() {
-    local cfg="${1}" # Configuration string.
-    if [ -z "${KEACTRL_CFG_FILE}" ]; then
+    local cfg="${1-}" # Configuration string.
+    if [ -z "${KEACTRL_CFG_FILE+x}" ]; then
         test_lib_error "create_keactrl_config requires KEACTRL_CFG_FILE \
 variable be set"
         clean_exit 1
@@ -388,7 +388,7 @@ configuration"
 # Sets Kea logger to write to the file specified by the global value
 # ${LOG_FILE}.
 set_logger() {
-    if [ -z "${LOG_FILE}" ]; then
+    if [ -z "${LOG_FILE+x}" ]; then
         test_lib_error "set_logger requires LOG_FILE variable be set"
         clean_exit 1
     fi
@@ -417,7 +417,7 @@ fi
 #   _GET_PID: holds a PID if process is running
 #   _GET_PIDS_NUM: holds 1 if process is running, 0 otherwise
 get_pid() {
-    local proc_name=${1       # Process name
+    local proc_name=${1-}       # Process name
     local cfg_file_name=${2-}   # Configuration file name without extension.
 
     # PID file name includes process name. The process name is required.
@@ -451,13 +451,23 @@ get_pid() {
 
 # Get the name of the process identified by PID.
 get_process_name() {
-    local pid=${1}
+    local pid=${1-}
+    if test -z "${pid}"; then
+        test_lib_error 'expected PID parameter in get_process_name'
+        clean_exit 1
+    fi
+
     ps "${pid}" | tr -s ' ' | cut -d ' ' -f 6- | head -n 2 | tail -n 1
 }
 
 # Wait for file to be created.
 wait_for_file() {
-    local file=${1}
+    local file=${1-}
+    if test -z "${file}"; then
+        test_lib_error 'expected file parameter in wait_for_file'
+        clean_exit 1
+    fi
+
     local timeout='4' # seconds
     local deadline=$(($(date +%s) + timeout))
     while ! test -f "${file}"; do
@@ -473,7 +483,12 @@ wait_for_file() {
 
 # Wait for process identified by PID to die.
 wait_for_process_to_stop() {
-    local pid=${1}
+    local pid=${1-}
+    if test -z "${pid}"; then
+        test_lib_error 'expected PID parameter in wait_for_process_to_stop'
+        clean_exit 1
+    fi
+
     local timeout='4' # seconds
     local deadline=$(($(date +%s) + timeout))
     while ps "${pid}" >/dev/null; do
@@ -499,7 +514,7 @@ wait_for_process_to_stop() {
 # names, add this before pgrep:
 # proc_name=$(printf '%s' "${proc_name}" | cut -c1-15)
 kill_processes_by_name() {
-    local proc_name=${1} # Process name
+    local proc_name=${1-}  # Process name
     if [ -z "${proc_name}" ]; then
         test_lib_error "kill_processes_by_name requires process name"
         clean_exit 1
@@ -626,7 +641,7 @@ cleanup() {
 # It performs the cleanup and prints whether the test has passed or failed.
 # If a test fails, the Kea log is dumped.
 clean_exit() {
-    exit_code=${1}  # Exit code to be returned by the exit function.
+    exit_code=${1-}  # Exit code to be returned by the exit function.
     case ${exit_code} in
         ''|*[!0-9]*)
             test_lib_error "argument passed to clean_exit must be a number" ;;
@@ -639,7 +654,7 @@ clean_exit() {
 # Starts Kea process in background using a configuration file specified
 # in the global variable ${CFG_FILE}.
 start_kea() {
-    local bin=${1}
+    local bin=${1-}
     if [ -z "${bin}" ]; then
         test_lib_error "binary name must be specified for start_kea"
         clean_exit 1
@@ -659,7 +674,11 @@ start_kea() {
 # Return value:
 #    _WAIT_FOR_KEA: 0 if Kea hasn't started, 1 otherwise
 wait_for_kea() {
-    local timeout=${1} # Desired timeout in seconds.
+    local timeout=${1-} # Desired timeout in seconds.
+    if test -z "${timeout}"; then
+        test_lib_error 'expected timeout parameter in wait_for_kea'
+        clean_exit 1
+    fi
     case ${timeout} in
         ''|*[!0-9]*)
             test_lib_error "argument passed to wait_for_kea must be a number"
@@ -691,9 +710,9 @@ wait_for_kea() {
 # Return value:
 #    _WAIT_FOR_MESSAGE: 0 if the message hasn't occurred, 1 otherwise.
 wait_for_message() {
-    local timeout=${1}     # Expected timeout value in seconds.
-    local message="${2}"   # Expected message id.
-    local occurrences=${3} # Number of expected occurrences.
+    local timeout="${1-}"      # Expected timeout value in seconds.
+    local message="${2-}"      # Expected message id.
+    local occurrences="${3-}"  # Number of expected occurrences.
 
     # Validate timeout
     case ${timeout} in
@@ -743,8 +762,12 @@ must be a number"
 #    _WAIT_FOR_SERVER_DOWN: 1 if server is down, 0 if timeout occurred and the
 #                             server is still running.
 wait_for_server_down() {
-    local timeout=${1}    # Timeout specified in seconds.
-    local proc_name=${2}  # Server process name.
+    local timeout="${1-}"    # Timeout specified in seconds.
+    local proc_name="${2-}"  # Server process name.
+    if test -z "${proc_name}"; then
+        test_lib_error 'expected process name parameter in wait_for_server_down'
+        clean_exit 1
+    fi
 
     case ${timeout} in
         ''|*[!0-9]*)
@@ -770,8 +793,8 @@ wait_for_server_down() {
 
 # Sends specified signal to the Kea process.
 send_signal() {
-    local sig=${1}       # Signal number.
-    local proc_name=${2} # Process name
+    local sig=${1-}       # Signal number.
+    local proc_name=${2-} # Process name
 
     # Validate signal
     case ${sig} in
@@ -803,8 +826,8 @@ must be a number"
 # live process it sets _SERVER_PID_FILE and _SERVER_PID to the
 # corresponding values.  Otherwise, it emits an error and exits.
 verify_server_pid() {
-    local bin_name="${1}" # binary name of the server
-    local cfg_file="${2}" # config file name
+    local bin_name="${1-}" # binary name of the server
+    local cfg_file="${2-}" # config file name
 
     # We will construct the PID file name based on the server config
     # and binary name
index b4315059bbf1268ead01e696ff036bb739d9f830..46787b627a21e5000e862ad4c1204e9398b747f4 100755 (executable)
 # used.
 set -eu
 
-report_file="$1"
-dest="$2"
+report_file="${1-}"
+dest="${2-}"
 
 if [ -z "${report_file}" ]
+then
+    echo "ERROR mk_cfgrpt.sh - expected report_file parameter"
+    exit 1
+fi
+
+if [ -z "${dest}" ]
+then
+    echo "ERROR mk_cfgrpt.sh - expected dest parameter"
+    exit 1
+fi
+
+if [ ! -f "${report_file}" ]
 then
     echo "ERROR mk_cfgrpt.sh - input report: $report_file does not exist"
     exit 1