[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20251017151256.111f2669@pumpkin>
Date: Fri, 17 Oct 2025 15:12:56 +0100
From: David Laight <david.laight.linux@...il.com>
To: Kevin Locke <kevin@...inlocke.name>
Cc: Jonathan Corbet <corbet@....net>, Randy Dunlap <rdunlap@...radead.org>,
Thorsten Leemhuis <linux@...mhuis.info>, linux-kernel@...r.kernel.org
Subject: Re: [PATCH] tools: remove unnecessary x suffix in test strings
On Thu, 16 Oct 2025 17:47:09 -0600
Kevin Locke <kevin@...inlocke.name> wrote:
> An "x" suffix was appended to test variable expansions, presumably to
> avoid issues with empty strings in some old shells, or perhaps with the
> intention of avoiding issues with dashes or other special characters
> that an "x" prefix might have avoided. In either case, POSIX ensures
> that such protections are not necessary, and are unlikely to be
> encountered in shells currently in use, as indicated by shellcheck
> SC2268.
>
> Remove the "x" suffixes which unnecessarily complicate the code.
>
> Signed-off-by: Kevin Locke <kevin@...inlocke.name>
> Suggested-by: David Laight <david.laight.linux@...il.com>
> ---
>
> Thanks David, that's a good point about the x suffixes. Since
> shellcheck warns about the x prefixes (SC2268) and I'm not aware of any
> shells currently in use which require them,
The problems arise when $1 is (say) "-x", a simple LR parser will treat
[ -x = -x ] as a check for the file "=" being executable and then give
a syntax error for the second -x.
I can't imagine why shellcheck should warn about a leading x (or any other
character) provided field splitting is disabled (eg by "").
The leading x has definitely been needed in the past.
POSIX does require the three argument 'test' look for the middle argument
being an operator - but there might be historic shells that don't so that.
OTOH you are probably looking for code from the early 1980s!
But the POSIX spec (last time I read it) does point out the problems
with arbitrary strings being treated as operators causing complex expressions
be mis-parsed - which a leading x fixes.
> I think they are safe to
> remove to clean up the code a bit. Here's a patch to do just that,
> which can be applied on top of my previous patch.
>
> Since -o is an XSI extension to POSIX, I've stuck with ||, but I think
> you are right that x would not be required in that case either.
I'm not sure there are any common shells that don't support -o and -a.
They get used quite a lot.
I'm pretty sure they were supported by the pre-POSIX System-V shells
(or the /bin/[ program they ran).
David
>
> Thanks again,
> Kevin
>
>
> tools/debugging/kernel-chktaint | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/tools/debugging/kernel-chktaint b/tools/debugging/kernel-chktaint
> index 051608a63d9f..051ac27b58eb 100755
> --- a/tools/debugging/kernel-chktaint
> +++ b/tools/debugging/kernel-chktaint
> @@ -18,8 +18,8 @@ retrieved from /proc/sys/kernel/tainted on another system.
> EOF
> }
>
> -if [ "$1"x != "x" ]; then
> - if [ "$1"x = "--helpx" ] || [ "$1"x = "-hx" ] ; then
> +if [ "$1" != "" ]; then
> + if [ "$1" = "--help" ] || [ "$1" = "-h" ] ; then
> usage
> exit 1
> elif [ $1 -ge 0 ] 2>/dev/null ; then
Powered by blists - more mailing lists