commit: f58674287f79ee537077cb17bad99ede0f3854c8
Author: Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Tue Oct 14 08:48:35 2025 +0000
Commit: Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Tue Oct 14 12:59:10 2025 +0000
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=f5867428
Initialise the hr variable in the hr() function
The hr() function composes a horizontal rule by appending characters to
the 'hr' variable, which is never initialised. One might think that it
matters not, given that the variable is declared as function-local.
Unfortunately, it does matter.
$ testcase='var=leaked; f() { local var; echo "var=${var-unset}"; }; f'
$ set -- bash 'busybox sh' ksh mksh yash dash
$ for sh; do printf '%s: ' "$sh"; $sh -c "$testcase"; done
bash: var=unset
busybox sh: var=unset
ksh: var=unset
mksh: var=unset
yash: var=unset
dash: var=leaked
It can be seen here that dash is an outlier. Such are the perils of
depending upon a feature whose behaviour is undefined by the Shell
Command Language specification. Address this issue by ensuring that the
'hr' variable is always initialised as the null string.
Fixes: 8a83cf36a847fbd32990d3590bfd22a1516af898
Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>
Signed-off-by: Sam James <sam <AT> gentoo.org>
functions.sh | 1 +
test-functions | 1 +
2 files changed, 2 insertions(+)
diff --git a/functions.sh b/functions.sh
index 4b25de7..5b49ce8 100644
--- a/functions.sh
+++ b/functions.sh
@@ -256,6 +256,7 @@ hr()
fi
c=${1--}
c=${c%"${c#?}"}
+ hr=
i=0
while [ "$(( i += 16 ))" -le "${length}" ]; do
hr=${hr}${c}${c}${c}${c}${c}${c}${c}${c}${c}${c}${c}${c}${c}${c}${c}${c}
diff --git a/test-functions b/test-functions
index 0aa2f21..641ac93 100755
--- a/test-functions
+++ b/test-functions
@@ -672,6 +672,7 @@ test_hr() {
expected=$1
shift
test_description="hr $(quote_args "$@")"
+ hr="leakage"
test "$(hr "$@")" = "${expected}"
}