test incorrectly rejecting valid expression with confusing ! placement
Hi,
Now that Herbert fixed the reported crash in test (in a far simpler manner than I had suggested, which I like), I did some more testing, and came across one case that does not currently work, and did not work in the past, but is perfectly valid:
$ src/dash -c 'test ! ! = !' src/dash: 1: test: =: unexpected operator
POSIX requires special behaviour for four-argument tests:
4 arguments:
If $1 is '!', negate the three-argument test of $2, $3, and $4. [...]
so this is supposed to evaluate as the negation of test ! = !. That test does work properly in dash.
There are also some cases where test gives incorrect results when combining ! with -o, but ( ), -a and -o have been obsoleted in favour of the built-in shell operators ( ), && and ||, so I am not sure if that is worth fixing. Details anyway:
$ src/dash -c 'test ! -o !' src/dash: 1: test: -o: unexpected operator
This is covered by the special rule for three arguments, where the second argument is a binary operator. -o is a boolean binary operator, but a binary operator nonetheless, so this should test whether the two ! arguments are empty strings.
$ src/dash -c 'test ! "" -o !'; echo $? 0
This is covered by the special rule for four arguments, there ! as the first argument evaluates the remaining three-argument test and negates the result. In this special case, ! does not have higher precedence than -o, so the correct exit status is 1, which bash's test gives.
Cheers,
Harald
Harald van Dijk dixit:
Quoted 4 lines
$ src/dash -c 'test ! ! = !' src/dash: 1: test: =: unexpected operatorPOSIX requires special behaviour for four-argument tests:
Right but…
Quoted 2 lines
There are also some cases where test gives incorrect results when combining ! with -o
… ‘-o’ is XSI, not POSIX. Do not expect…
Quoted 1 line
$ src/dash -c 'test ! "" -o !'; echo $?
… this to work.
Quoted 4 lines
This is covered by the special rule for four arguments, there ! as the first argument evaluates the remaining three-argument test and negates the result. In this special case, ! does not have higher precedence than -o, so the correct exit status is 1, which bash's test gives.
Only for a shell implementing the XSI ‘-o’ for POSIX test. You shouldn’t use -o anyway as it’s deprecated.
bye,
//mirabilos
Signature · 4 lines
FWIW, I'm quite impressed with mksh interactively. I thought it was much *much* more bare bones. But it turns out it beats the living hell out of ksh93 in that respect. I'd even consider it for my daily use if I hadn't wasted half my life on my zsh setup. :-) -- Frank Terbeck in #!/bin/mksh
Quoted 16 lines · Thorsten Glaser
Harald van Dijk dixit:
Quoted 4 lines
$ src/dash -c 'test ! ! = !' src/dash: 1: test: =: unexpected operatorPOSIX requires special behaviour for four-argument tests:
Right but…
Quoted 2 lines
There are also some cases where test gives incorrect results when combining ! with -o
… ‘-o’ is XSI, not POSIX. Do not expect…
Quoted 1 line
$ src/dash -c 'test ! "" -o !'; echo $?
Quoted 8 lines
… this to work.
Quoted 4 lines
This is covered by the special rule for four arguments, there ! as the first argument evaluates the remaining three-argument test and negates the result. In this special case, ! does not have higher precedence than -o, so the correct exit status is 1, which bash's test gives.
Only for a shell implementing the XSI ‘-o’ for POSIX test.
That is correct, but dash is such a shell. It attempts to implement -o, -a, and \( ... \), and does so mostly correctly.
Quoted 1 line
You shouldn’t use -o anyway as it’s deprecated.
Quoted 4 lines · Indeed. I made a note that it has been obsoleted. I
There are also some cases where test gives incorrect results when combining ! with -o, but ( ), -a and -o have been obsoleted in favour of the built-in shell operators ( ), && and ||, so I am not sure if that is worth fixing.
Even if it had not been obsoleted, it would be better not to use -a and -o. In fact, I have trouble imagining why ! has not also been obsoleted in favour of a simple logical negation of the test (test x != y instead of test ! x == y, which is only possible for some of the tests), or the shell's built-in ! (! test x == y instead of test ! x == y, which is always possible).
Cheers,
Harald
Quoted 9 lines · Harald van Dijk
Hi,
Now that Herbert fixed the reported crash in test (in a far simpler manner than I had suggested, which I like), I did some more testing, and came across one case that does not currently work, and did not work in the past, but is perfectly valid:
$ src/dash -c 'test ! ! = !' src/dash: 1: test: =: unexpected operator
Agreed.
Quoted 2 lines
$ src/dash -c 'test ! -o !' src/dash: 1: test: -o: unexpected operator
Nope, the rule is quite clear that it only applies to binary primaries, not operators. -o is an operator.
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Quoted 11 lines · Herbert Xu
Quoted 9 lines · Harald van Dijk
Hi,
Now that Herbert fixed the reported crash in test (in a far simpler manner than I had suggested, which I like), I did some more testing, and came across one case that does not currently work, and did not work in the past, but is perfectly valid:
$ src/dash -c 'test ! ! = !' src/dash: 1: test: =: unexpected operator
Quoted 7 lines
Agreed.
Quoted 2 lines
$ src/dash -c 'test ! -o !' src/dash: 1: test: -o: unexpected operatorNope, the rule is quite clear that it only applies to binary primaries, not operators. -o is an operator.
Huh?
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html
states that there are only two operators "!" and "()", and specifically mentions that -a and -o are binary primaries:
expression1 -a expression2
[OB XSI] [Option Start]
True if both expression1 and expression2 are true; otherwise, false.
The -a binary primary is left associative. It has a higher precedence than -o. [Option End]
expression1 -o expression2
[OB XSI] [Option Start]
True if either expression1 or expression2 is true; otherwise, false.
The -o binary primary is left associative. [Option End]
"test ! -o !" is a three-argument test, where $2 (-o) is a binary primary, so it is the binary test of $1 and $3, and the end result is an exit status of 0. Bash and ksh get it right, dash fails.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 4 lines · Eric Blake
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html
states that there are only two operators "!" and "()", and specifically mentions that -a and -o are binary primaries:
Well, a primary is usually something that cannot be reduced further when discussing syntax. As both -a and -o take expressions on either side, it is highly unusual to designate them as primaries instead of operators.
Also, in the rationale it clearly states that -a/-o are operators and not primaries.
In any case, dash does not claim XSI conformance, so we're at liberty to choose the most suitable implementation of an existing feature that we inherited from BSD.
Quoted 3 lines
"test ! -o !" is a three-argument test, where $2 (-o) is a binary primary, so it is the binary test of $1 and $3, and the end result is an exit status of 0. Bash and ksh get it right, dash fails.
Both zsh and posh do it like dash:
$ zsh -c 'test ! -o !'; echo $? zsh:test:1: too many arguments 1 $ zsh -c 'test ! = !'; echo $? 0 $
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Quoted 5 lines · Herbert Xu
Quoted 4 lines
"test ! -o !" is a three-argument test, where $2 (-o) is a binary primary, so it is the binary test of $1 and $3, and the end result is an exit status of 0. Bash and ksh get it right, dash fails.
Quoted 8 lines
Both zsh and posh do it like dash:
$ zsh -c 'test ! -o !'; echo $? zsh:test:1: too many arguments 1 $ zsh -c 'test ! = !'; echo $? 0 $
Another case in point is 'test ! = -o a', here bash differs with every other shell installed on my system, which all return 0, just like dash.
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
[adding the Austin Group]
Quoted 15 lines · Herbert Xu
Quoted 5 lines · Herbert Xu
Quoted 4 lines
"test ! -o !" is a three-argument test, where $2 (-o) is a binary primary, so it is the binary test of $1 and $3, and the end result is an exit status of 0. Bash and ksh get it right, dash fails.
Quoted 8 lines
Both zsh and posh do it like dash:
$ zsh -c 'test ! -o !'; echo $? zsh:test:1: too many arguments 1 $ zsh -c 'test ! = !'; echo $? 0 $
Quoted 3 lines
Another case in point is 'test ! = -o a', here bash differs with every other shell installed on my system, which all return 0, just like dash.
GNU coreutils behaves like bash; the POSIX wording justifies bash:
4 arguments:
If $1 is '!', negate the three-argument test of $2, $3, and $4.
[OB XSI] [Option Start] If $1 is '(' and $4 is ')', perform the
two-argument test of $2 and $3. [Option End] On systems that do not support the XSI option, the results are unspecified if $1 is '(' and $4 is ')'.
Otherwise, the results are unspecified.
That is, "test ! = -o a" should be parsed as "test ! \( = -o a \)" (bash's behavior, per the first bullet), not "test \( ! = \) -o a" (ksh's behavior, per the third bullet).
Yet another reason why the XSI -a and -o make the parsing precedence of test a pain to decipher. Do we need to open yet another bug against POSIX to allow for ksh's behavior?
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 8 lines · Herbert Xu
Quoted 7 lines · Harald van Dijk
Hi,
Now that Herbert fixed the reported crash in test (in a far simpler manner than I had suggested, which I like), I did some more testing, and came across one case that does not currently work, and did not work in the past, but is perfectly valid:
Quoted 4 lines
Quoted 2 lines
$ src/dash -c 'test ! ! = !' src/dash: 1: test: =: unexpected operatorAgreed.
This case is now fixed by the following patch:
commit 4f7e4c8201e580b9d31c09d8a484741072033c01
Author: Herbert Xu <[email protected]> Date: Sun Sep 28 18:40:18 2014 +0800
[BUILTIN] Correctly handle test ! ! = !
This patch adds a special case in testcmd for the 4-argument expression beginning with a !. Without this ! ! = ! is deemed a syntax error, which breaks POSIX.
Note that this special case does not extend down into subexpressions so if ! ! = ! is used inside parentheses then a syntax error will still occur as before.
ChangeLog +4 -0
@@ -1,3 +1,7 @@ +2014-09-28 Herbert Xu <[email protected]> + + * Correctly handle test ! ! = !. + 2014-09-26 Herbert Xu <[email protected]> * Small optimisation of command -pv change.
src/bltin/test.c +7 -3
@@ -177,7 +177,7 @@ testcmd(int argc, char **argv) {const struct t_op *op;enum token n; -int res; +int res =1 ;if (*argv[0 ] =='[' ) {if (*argv[--argc] !=']' )
@@ -185,11 +185,12 @@ testcmd(int argc, char **argv) argv[argc] =NULL ; } +recheck: argv++; argc--;if (argc <1 ) -return 1 ; +return res;/* * POSIX prescriptions: he who wrote this deserves the Nobel
@@ -209,6 +210,9 @@ testcmd(int argc, char **argv) argv[--argc] =NULL ; argv++; argc--; + }else if (!strcmp (argv[0 ],"!" )) { + res =0 ; + goto recheck; } }
@@ -216,7 +220,7 @@ testcmd(int argc, char **argv) eval: t_wp = argv; - res = !oexpr (n); + res ^=oexpr (n); argv = t_wp;if (argv[0 ] !=NULL && argv[1 ] !=NULL )
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt