[U-Boot] [PATCH] hush: when a variable expansion is empty, don't drop the parameter

Russell King - ARM Linux linux at arm.linux.org.uk
Thu Feb 27 11:39:24 CET 2014


On Wed, Feb 26, 2014 at 11:55:06PM -0700, Stephen Warren wrote:
> 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 any empty expansion with an empty pair
> of quotes, so that an argument is still generated.
> 
> Reported-by: Russell King <linux at arm.linux.org.uk>
> Signed-off-by: Stephen Warren <swarren at wwwdotorg.org>
> ---
> Hmm. I wonder if this causes other problems, such as:
> 
> if test -z $x; then echo "zero"; else echo "non-zero"; fi
> 
> I guess we should only quote the expansion if the input string had quotes
> around the text being expanded, but I'm not sure if we can know that.
> Perhaps more investigation is needed:-( Although... I /guess/ that case
> is rarer, so this patch still improves things?

Standard shell will only drop an empty expansion if it's unquoted, so:

	a="1 2 3"
	b=4
	c=

	echo $a $b $c

ends up with arg1 = "1", arg2 = "2", arg3 = "3", arg4 = "4" and no arg5

	echo "$a" "$b" "$c"

ends up with arg1 = "1 2 3", arg2 = "4", arg3 = "" and no arg4

	echo "$a $b $c"

ends up with arg1 = "1 2 3 4 " and no arg2

If you'd like to check out this behaviour, this script will help you see
each argument as it was passed by the shell.

#!/bin/sh
n=1
for a in "$@"; do
  echo "arg$n: \"$a\""
  n=$(($n + 1))
done

Getting this right matters, because it affects stuff like the ability
to make use of test -z / test -n, while preserving the ability to have
unquoted empty variables disappear from argument lists.

-- 
FTTC broadband for 0.8mile line: now at 9.7Mbps down 460kbps up... slowly
improving, and getting towards what was expected from it.


More information about the U-Boot mailing list