[PATCH 1/3] selftests: use local variables consistently in runner.sh

From: Jori Koolstra (CIQ)

Date: Wed Nov 18 2026 - 08:40:56 EST


run_one() and run_many() assign their working variables without
declaring them local, so they leak into the global namespace of
whichever shell sources runner.sh, i.e. run_kselftest.sh or the
RUN_TESTS recipe in lib.mk.

Declare these variables local so that local is used consistently
throughout the file and the sourcing shell's namespace is not polluted.
This does not add a new requirement on the shell: local is already used
in run_many(), in_netns() and run_in_netns().

Also, obey the convention to use upper case names for variables that are
constant once assigned.

Signed-off-by: Jori Koolstra (CIQ) <jkoolstra@xxxxxxxxx>
---
tools/testing/selftests/kselftest/runner.sh | 29 ++++++++++++++++++-----------
1 file changed, 18 insertions(+), 11 deletions(-)

diff --git a/tools/testing/selftests/kselftest/runner.sh b/tools/testing/selftests/kselftest/runner.sh
index 45fb8ec38493..9b1dad5441ea 100644
--- a/tools/testing/selftests/kselftest/runner.sh
+++ b/tools/testing/selftests/kselftest/runner.sh
@@ -58,13 +58,18 @@ tap_timeout()

run_one()
{
+ local DIR TEST BASENAME_TEST BASENAME_SANITIZED SETTINGS TEST_HDR_MSG
+ local line field value interpreter stdbuf cmd rc
+
DIR="$1"
TEST="$2"

BASENAME_TEST=$(basename $TEST)

# Reset any "settings"-file variables.
- export kselftest_timeout="$kselftest_default_timeout"
+ local kselftest_timeout="$kselftest_default_timeout"
+ # Make kselftest_timeout variables available to tests
+ export kselftest_timeout

# Safe default if tr not available
kselftest_cmd_args_ref="KSELFTEST_ARGS"
@@ -91,8 +96,8 @@ run_one()
fi

# Load per-test-directory kselftest "settings" file.
- settings="$BASE_DIR/$DIR/settings"
- if [ -r "$settings" ] ; then
+ SETTINGS="$BASE_DIR/$DIR/settings"
+ if [ -r "$SETTINGS" ] ; then
while read line ; do
# Skip comments.
if echo "$line" | grep -q '^#'; then
@@ -100,8 +105,8 @@ run_one()
fi
field=$(echo "$line" | cut -d= -f1)
value=$(echo "$line" | cut -d= -f2-)
- eval "kselftest_$field"="$value"
- done < "$settings"
+ eval "local kselftest_$field"="$value"
+ done < "$SETTINGS"
fi

# Command line timeout overrides the settings file
@@ -164,8 +169,8 @@ run_one()

in_netns()
{
- local name=$1
- ip netns exec $name bash <<-EOF
+ local NAME=$1
+ ip netns exec $NAME bash <<-EOF
BASE_DIR=$BASE_DIR
source $BASE_DIR/kselftest/runner.sh
logfile=$logfile
@@ -175,9 +180,9 @@ in_netns()

run_in_netns()
{
- local tmplog="/tmp/$(mktemp -u ${BASENAME_TEST}-XXXXXX)"
- local netns=$(mktemp -u ${BASENAME_TEST}-XXXXXX)
- local rc
+ local tmplog netns rc
+ tmplog="/tmp/$(mktemp -u ${BASENAME_TEST}-XXXXXX)"
+ netns=$(mktemp -u ${BASENAME_TEST}-XXXXXX)

ip netns add $netns
if [ $? -ne 0 ]; then
@@ -198,8 +203,10 @@ run_in_netns()

run_many()
{
+ local DIR TEST BASENAME_TEST
+ local pids pid rc
+
DIR="${PWD#${BASE_DIR}/}"
- local rc
pids=

for TEST in "$@"; do

--
2.56.1