cmd: test: add bug-compatibility special case for 'test -n'
Simon Glass
sjg at chromium.org
Thu Apr 2 20:27:30 CEST 2026
On 2026-03-30T14:01:06, Rasmus Villemoes <ravi at prevas.dk> wrote:
> cmd: test: add bug-compatibility special case for 'test -n'
> 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: 8b0619579b2 ("cmd: test: fix handling of single-argument form of test")
> Reported-by: Franz Schnyder <franz.schnyder at toradex.com>
> Signed-off-by: Rasmus Villemoes <ravi at prevas.dk>
>
> cmd/test.c | 15 ++++++++++++++-
> test/hush/if.c | 8 ++++++++
> 2 files changed, 22 insertions(+), 1 deletion(-)
Reviewed-by: Simon Glass <sjg at chromium.org>
More information about the U-Boot
mailing list