[PATCH 3/9] ktest: Treat undefined self-reference as empty

Ricardo B. Marlière posted 9 patches 1 month ago
[PATCH 3/9] ktest: Treat undefined self-reference as empty
Posted by Ricardo B. Marlière 1 month ago
Config variables are expanded when they are assigned. A first-time append
such as:

  VAR := ${VAR} foo

leaves the literal ${VAR} in the stored value because VAR has not been
defined yet. Later expansions then carry the self-reference forward instead
of behaving like an empty prefix.

Drop an unescaped self-reference when the variable has no current value,
and trim the outer whitespace left behind. Keep escaped \${VAR} references
unchanged so literal text still works.

Signed-off-by: Ricardo B. Marlière <rbm@suse.com>
---
 tools/testing/ktest/ktest.pl | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/tools/testing/ktest/ktest.pl b/tools/testing/ktest/ktest.pl
index b7a1c8c617e0..b8fcdabffffe 100755
--- a/tools/testing/ktest/ktest.pl
+++ b/tools/testing/ktest/ktest.pl
@@ -910,6 +910,14 @@ sub set_variable {
     if (defined($command_tmp_vars{$lvalue})) {
 	return;
     }
+
+    # If a variable is undefined, treat an unescaped self-reference as empty.
+    if (!defined($variable{$lvalue})) {
+	$rvalue =~ s/(?<!\\)\$\{\Q$lvalue\E\}//g;
+	$rvalue =~ s/^\s+//;
+	$rvalue =~ s/\s+$//;
+    }
+
     if ($rvalue =~ /^\s*$/) {
 	delete $variable{$lvalue};
     } else {

-- 
2.53.0

Re: [PATCH 3/9] ktest: Treat undefined self-reference as empty
Posted by Steven Rostedt 1 month ago
On Sat, 07 Mar 2026 19:07:58 -0300
Ricardo B. Marlière <rbm@suse.com> wrote:

> Config variables are expanded when they are assigned. A first-time append
> such as:
> 
>   VAR := ${VAR} foo
> 
> leaves the literal ${VAR} in the stored value because VAR has not been
> defined yet. Later expansions then carry the self-reference forward instead
> of behaving like an empty prefix.
> 
> Drop an unescaped self-reference when the variable has no current value,
> and trim the outer whitespace left behind. Keep escaped \${VAR} references
> unchanged so literal text still works.
> 
> Signed-off-by: Ricardo B. Marlière <rbm@suse.com>
> ---
>  tools/testing/ktest/ktest.pl | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/tools/testing/ktest/ktest.pl b/tools/testing/ktest/ktest.pl
> index b7a1c8c617e0..b8fcdabffffe 100755
> --- a/tools/testing/ktest/ktest.pl
> +++ b/tools/testing/ktest/ktest.pl
> @@ -910,6 +910,14 @@ sub set_variable {
>      if (defined($command_tmp_vars{$lvalue})) {
>  	return;
>      }
> +
> +    # If a variable is undefined, treat an unescaped self-reference as empty.
> +    if (!defined($variable{$lvalue})) {
> +	$rvalue =~ s/(?<!\\)\$\{\Q$lvalue\E\}//g;

So today I learned about "negative look behind" "?<!" ;-)

-- Steve

> +	$rvalue =~ s/^\s+//;
> +	$rvalue =~ s/\s+$//;
> +    }
> +
>      if ($rvalue =~ /^\s*$/) {
>  	delete $variable{$lvalue};
>      } else {
>