diff options
author | Massimiliano Minella <massimiliano.minella@se.com> | 2024-02-08 15:58:27 +0100 |
---|---|---|
committer | Tom Rini <trini@konsulko.com> | 2024-03-02 12:26:19 -0500 |
commit | 4c7363068651f222be6150050bb3aa9823ca6f78 (patch) | |
tree | d105f051213ba03aaf0b50a1bced135a463a7a4a /cmd | |
parent | fd50ae3f2628d1197829e0c2ef52aed653e80d68 (diff) | |
download | u-boot-4c7363068651f222be6150050bb3aa9823ca6f78.tar.gz |
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>
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/setexpr.c | 9 |
1 files changed, 4 insertions, 5 deletions
diff --git a/cmd/setexpr.c b/cmd/setexpr.c index 233471f6cb..ab76824a32 100644 --- a/cmd/setexpr.c +++ b/cmd/setexpr.c @@ -216,14 +216,12 @@ int setexpr_regex_sub(char *data, uint data_size, char *nbuf, uint nbuf_size, if (res == 0) { if (loop == 0) { debug("%s: No match\n", data); - return 1; } else { - break; + debug("## MATCH ## %s\n", data); } + break; } - debug("## MATCH ## %s\n", data); - if (!s) return 1; @@ -540,7 +538,8 @@ U_BOOT_CMD( " - For each substring matching the regular expression <r> in the\n" " string <t>, substitute the string <s>. The result is\n" " assigned to <name>. If <t> is not supplied, use the old\n" - " value of <name>\n" + " value of <name>. If no substring matching <r> is found in <t>,\n" + " assign <t> to <name>.\n" "setexpr name sub r s [t]\n" " - Just like gsub(), but replace only the first matching substring" #endif |