cmd: setexpr: fix no matching string in gsub return empty value

In gsub, when the destination string is empty, the string 't' is
provided and the regular expression doesn't match, then the final result
is an empty string.

Example:

=> echo ${foo}

=> setenv foo
=> setexpr foo gsub e a bar
=> echo ${foo}

=>

The variable ${foo} should contain "bar" and the lack of match shouldn't
be considered an error.

This patch fixes the erroneous behavior by removing the return
statement and breaking out of the loop in case of lack of match.

Also add a test for the no match case.

Signed-off-by: Massimiliano Minella <massimiliano.minella@se.com>
This commit is contained in:
Massimiliano Minella
2024-03-02 12:26:19 -05:00
committed by Tom Rini
parent fd50ae3f26
commit 4c73630686
3 changed files with 15 additions and 5 deletions
+10
View File
@@ -179,6 +179,16 @@ static int setexpr_test_regex(struct unit_test_state *uts)
val = env_get("mary");
ut_asserteq_str("this is a test", val);
/* No match */
ut_assertok(run_command("setenv fred 'this is a test'", 0));
ut_assertok(run_command("setenv mary ''", 0));
ut_assertok(run_command("setexpr fred gsub us is \"${fred}\"", 0));
ut_assertok(run_command("setexpr mary gsub us is \"${fred}\"", 0));
val = env_get("fred");
ut_asserteq_str("this is a test", val);
val = env_get("mary");
ut_asserteq_str("this is a test", val);
unmap_sysmem(buf);
return 0;