]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Fix get_ports.sh script
authorOndřej Surý <ondrej@isc.org>
Tue, 5 May 2020 09:11:57 +0000 (11:11 +0200)
committerOndřej Surý <ondrej@isc.org>
Tue, 5 May 2020 10:27:47 +0000 (12:27 +0200)
There were two errors:

1. get_random() function was returning random number with leading zeros
   that could lead the shell to interpret the number as octal value
   instead of decimal.  The surrounding whitespace was also causing
   problems.

2. The calculation of the port was off, it was adding the whole range
   and not just the min port to the base.

bin/tests/system/get_ports.sh

index 30c066b2b42fca88bdaeccfddb45eb9644b88652..b2be6ca400b2378d043933a2285b83bb79d7fd19 100755 (executable)
@@ -18,9 +18,10 @@ statefile=get_ports.state
 port_min=5001
 port_max=32767
 
-get_random() {
-    dd if=/dev/urandom bs=1 count=2 2>/dev/null | od -tu2 -An
-}
+get_random() (
+    # shellcheck disable=SC2005,SC2046
+    echo $(dd if=/dev/urandom bs=1 count=2 2>/dev/null | od -tu2 -An) | sed -e 's/^0*//'
+)
 
 get_port() {
     tries=10
@@ -36,7 +37,8 @@ get_port() {
                    port="$1"
                else
                    port_range=$((port_max-port_min))
-                   port=$(($(get_random)%port_range+port_range))
+                   port_random=$(get_random)
+                   port=$((port_random%port_range+port_min))
                fi
            fi