mirror of
https://source.denx.de/u-boot/u-boot.git
synced 2026-06-02 09:46:37 +03:00
cmd: test: add bug-compatibility special case for 'test -n'
It turns out that there is lots of code in the wild, including in the
U-Boot tree itself, which used to rely on
test -n $somevar
to yield false when $somevar is not defined or empty. See for example
all the occurrences of 'test -n $fdtfile'. That was really only a
quirk of the implementation that refused calls with argc < 3, and not
because it was interpreted as
test -n "$somevar"
which is how this should be spelled.
While not exactly conforming to POSIX, we can accomodate such scripts
by special-casing a single argument "-n" to be interpreted as if it
comes from code as above with empty $somevar.
Since we only just added the ability to test a string for emptiness
using the single-argument form, it is very unlikely that there is code
doing
test "$str"
which would now fail if $str happens to be exactly "-n"; such a test
should really always be spelled
test -n "$str"
Fixes: 8b0619579b ("cmd: test: fix handling of single-argument form of test")
Reported-by: Franz Schnyder <franz.schnyder@toradex.com>
Signed-off-by: Rasmus Villemoes <ravi@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
committed by
Tom Rini
parent
d1cd673391
commit
f7e7c55e53
15
cmd/test.c
15
cmd/test.c
@@ -75,12 +75,25 @@ static int do_test(struct cmd_tbl *cmdtp, int flag, int argc,
|
||||
* Per POSIX, 'test' with 0 arguments should return 1, while
|
||||
* 'test <arg>' should be equivalent to 'test -n <arg>',
|
||||
* i.e. true if and only if <arg> is not empty.
|
||||
*
|
||||
* However, due to previous versions of U-Boot unconditionally
|
||||
* returning false when 'test' was given less than two
|
||||
* arguments, there are existing scripts that do
|
||||
*
|
||||
* test -n $somevar
|
||||
*
|
||||
* (i.e. without properly quoting $somevar) and expecting that
|
||||
* to return false when $somevar expands to nothing. It is
|
||||
* quite unlikely that anyone would use the single-argument
|
||||
* form to test a string for being empty and a possible
|
||||
* non-empty value for that string to be exactly "-n". So we
|
||||
* interpret 'test -n' as if it was 'test -n ""'.
|
||||
*/
|
||||
if (argc < 2)
|
||||
return 1;
|
||||
|
||||
if (argc == 2)
|
||||
return !strcmp(argv[1], "");
|
||||
return !strcmp(argv[1], "") || !strcmp(argv[1], "-n");
|
||||
|
||||
#ifdef DEBUG
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user