diff options
author | Stephen Warren <swarren@wwwdotorg.org> | 2014-03-01 22:16:10 -0700 |
---|---|---|
committer | Tom Rini <trini@ti.com> | 2014-03-07 10:59:06 -0500 |
commit | fe9ca3d3287185e388de55904420cc7915e4a3b1 (patch) | |
tree | 6ba29043f99cae9161808aa600da28f145029f91 /test/command_ut.c | |
parent | eebd1b58d05aa3719aa23be3e4a5c1424b26ff11 (diff) | |
download | u-boot-imx-fe9ca3d3287185e388de55904420cc7915e4a3b1.zip u-boot-imx-fe9ca3d3287185e388de55904420cc7915e4a3b1.tar.gz u-boot-imx-fe9ca3d3287185e388de55904420cc7915e4a3b1.tar.bz2 |
hush: fix some quoted variable expansion issues
The following shell command fails:
if test -z "$x"; then echo "zero"; else echo "non-zero"; fi
(assuming $x does not exist, it prints "non-zero" rather than "zero").
... since "$x" expands to nothing, and the argument is completely
dropped, causing too few to be passed to -z, causing cmd_test() to
error out early.
This is because when variable expansions are processed by make_string(),
the expanded results are concatenated back into a new string. However,
no quoting is applied when doing so, so any empty variables simply don't
generate any parameter when the combined string is parsed again.
Fix this by explicitly replacing quoting any argument that was originally
quoted when re-generating a string from the already-parsed argument list.
This also fixes loss of whitespace in commands such as:
setenv space " "
setenv var " 1${space}${space} 2 "
echo ">>${var}<<"
Reported-by: Russell King <linux@arm.linux.org.uk>
Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Stephen Warren <swarren@wwwdotorg.org>
Diffstat (limited to 'test/command_ut.c')
-rw-r--r-- | test/command_ut.c | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/test/command_ut.c b/test/command_ut.c index 4f42056..b6b6976 100644 --- a/test/command_ut.c +++ b/test/command_ut.c @@ -138,6 +138,23 @@ static int do_ut_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) HUSH_TEST(or_1_0_inv_inv, "! ! aaa = aaa -o ! ! bbb != bbb", y); HUSH_TEST(or_1_1_inv_inv, "! ! aaa = aaa -o ! ! bbb = bbb", y); + setenv("ut_var_nonexistent", NULL); + setenv("ut_var_exists", "1"); + HUSH_TEST(z_varexp_quoted, "-z \"$ut_var_nonexistent\"", y); + HUSH_TEST(z_varexp_quoted, "-z \"$ut_var_exists\"", n); + setenv("ut_var_exists", NULL); + + run_command("setenv ut_var_space \" \"", 0); + assert(!strcmp(getenv("ut_var_space"), " ")); + run_command("setenv ut_var_test $ut_var_space", 0); + assert(!getenv("ut_var_test")); + run_command("setenv ut_var_test \"$ut_var_space\"", 0); + assert(!strcmp(getenv("ut_var_test"), " ")); + run_command("setenv ut_var_test \" 1${ut_var_space}${ut_var_space} 2 \"", 0); + assert(!strcmp(getenv("ut_var_test"), " 1 2 ")); + setenv("ut_var_space", NULL); + setenv("ut_var_test", NULL); + #ifdef CONFIG_SANDBOX /* * File existence |