[PATCH 15/16] tests: Guard cleanup() against empty outdir to prevent rm /*

Arnaldo Carvalho de Melo <[email protected]> Mon, 22 Jun 2026 17:24:38 -0300
Newsgroups org.kernel.vger.dwarves
Message-ID <[email protected]>
From: Arnaldo Carvalho de Melo <[email protected]>

If make_tmpdir() fails, outdir is empty.  The cleanup() function
executes rm ${outdir}/* which expands to rm /* when outdir is unset or
empty — potentially deleting the entire root filesystem.

Add a guard to check that outdir is non-empty and points to an existing
directory before attempting removal.

Fixes: 52dbfb0b39595ed8 ("pahole: Refactor selftests")
Reported-by: Sashiko:gemini-3-1-pro-preview # Running on a local machine
Assisted-by: Claude:claude-opus-4-6
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
 tests/test_lib.sh | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/tests/test_lib.sh b/tests/test_lib.sh
index fb724329b6079aa2..19c90e897d9bff53 100755
--- a/tests/test_lib.sh
+++ b/tests/test_lib.sh
@@ -176,7 +176,9 @@ test_skip()
 
 cleanup()
 {
-	rm ${outdir}/*
-	rmdir $outdir
+	if [ -n "$outdir" ] && [ -d "$outdir" ]; then
+		rm ${outdir}/*
+		rmdir $outdir
+	fi
 	return 0
 }
-- 
2.54.0