zsh-workers
 help / color / mirror / code / Atom feed
* test is not posix
@ 2016-01-20 21:06 Trek
  2016-01-20 21:38 ` Bart Schaefer
  2016-01-20 23:46 ` Martijn Dekker
  0 siblings, 2 replies; 7+ messages in thread
From: Trek @ 2016-01-20 21:06 UTC (permalink / raw)
  To: zsh-workers

the -n and -z options of the test utility are not posixly correct if the
argument is "!", "(", ")" or "-a"

with a posix shell:

  $ sh -c 'test -n "!" && echo ok'
  ok

with zsh called as sh:

  $ zsh/sh -c 'test -n "!" && echo ok'
  zsh:test:1: too many arguments

a workaround exist:

  $ zsh/sh -c 'test "X!" != "X" && echo ok'
  ok

but the posix standard requires -n and -z to be safe even if the
argument is '!' or '(':

  The two commands: test "$1" and test ! "$1" could not be used reliably
  on some historical systems. Unexpected results would occur if such a
  string expression were used and $1 expanded to '!', '(', or a known
  unary primary. Better constructs are: test -n "$1" and test -z "$1"
  respectively. 

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html#tag_20_128_16


I discovered this bug developing a small shell library, where you can
find an use case in the n_str_set function of the lib/str.sh file
included in http://www.trek.eu.org/devel/naive/naive-0.0.2.tar.gz

c-ya!


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: test is not posix
  2016-01-20 21:06 test is not posix Trek
@ 2016-01-20 21:38 ` Bart Schaefer
  2016-01-20 21:44   ` Trek
  2016-01-20 23:46 ` Martijn Dekker
  1 sibling, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2016-01-20 21:38 UTC (permalink / raw)
  To: Trek, zsh-workers

On Jan 20, 10:06pm, Trek wrote:
}
} the -n and -z options of the test utility are not posixly correct if the
} argument is "!", "(", ")" or "-a"

I'm not able to reproduce this; what is the $ZSH_VERSION or $ZSH_PATCHLEVEL
that you are testing?  What do you see if you run with the -x option?

% ARGV0=sh Src/zsh -c 'test -n "!" && echo OK'
OK
% 



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: test is not posix
  2016-01-20 21:38 ` Bart Schaefer
@ 2016-01-20 21:44   ` Trek
  0 siblings, 0 replies; 7+ messages in thread
From: Trek @ 2016-01-20 21:44 UTC (permalink / raw)
  To: zsh-workers

On Wed, 20 Jan 2016 13:38:58 -0800
Bart Schaefer <schaefer@brasslantern.com> wrote:

> On Jan 20, 10:06pm, Trek wrote:
> } the -n and -z options of the test utility are not posixly correct
> if the } argument is "!", "(", ")" or "-a"
> 
> I'm not able to reproduce this; what is the $ZSH_VERSION or
> $ZSH_PATCHLEVEL that you are testing?  What do you see if you run
> with the -x option?

sorry, I missed to include that I'm using the 5.0.7-5 version included
in Debian GNU/Linux 8 (jessie)

> % ARGV0=sh Src/zsh -c 'test -n "!" && echo OK'
> OK

happy to see that it is already fixed in the latest version, where on my
system it prints the same error reported

c-ya!


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: test is not posix
  2016-01-20 21:06 test is not posix Trek
  2016-01-20 21:38 ` Bart Schaefer
@ 2016-01-20 23:46 ` Martijn Dekker
  2016-01-21 12:41   ` Peter Stephenson
  1 sibling, 1 reply; 7+ messages in thread
From: Martijn Dekker @ 2016-01-20 23:46 UTC (permalink / raw)
  To: Trek, zsh-workers

Trek schreef op 20-01-16 om 22:06:
> with zsh called as sh:
> 
>   $ zsh/sh -c 'test -n "!" && echo ok'
>   zsh:test:1: too many arguments

I think this might be the same bug that I reported back in May with
  test -n '('
and
  test -n ')'
http://www.zsh.org/mla/workers/2015/msg01225.html

Peter Stephenson fixed it less than 45 minutes later:
http://www.zsh.org/mla/workers/2015/msg01230.html

I hadn't noticed it fails on a single exclamation point as well.
It is in zsh 5.0.6 and 5.0.7. Got fixed in 5.0.8.

A lot more POSIX bugs have been fixed since, so the latest version is
recommended if you care about POSIX compliance.

> I discovered this bug developing a small shell library, where you can
> find an use case in the n_str_set function of the lib/str.sh file
> included in http://www.trek.eu.org/devel/naive/naive-0.0.2.tar.gz

As luck would have it, I'm developing a shell library too, including a
whole battery of bug and feature tests for POSIX shells. We should
exchange notes. I won't have time for the next few days, but I'll be in
touch.

Thanks,

- Martijn


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: test is not posix
  2016-01-20 23:46 ` Martijn Dekker
@ 2016-01-21 12:41   ` Peter Stephenson
  2016-01-21 13:06     ` [BUG] 'test' returns 1 (not >1) on error [was: test is not posix] Martijn Dekker
  0 siblings, 1 reply; 7+ messages in thread
From: Peter Stephenson @ 2016-01-21 12:41 UTC (permalink / raw)
  To: zsh-workers

On Thu, 21 Jan 2016 00:46:47 +0100
Martijn Dekker <martijn@inlv.org> wrote:
> Trek schreef op 20-01-16 om 22:06:
> > with zsh called as sh:
> > 
> >   $ zsh/sh -c 'test -n "!" && echo ok'
> >   zsh:test:1: too many arguments
> 
> I think this might be the same bug that I reported back in May with
>   test -n '('
> and
>   test -n ')'
> http://www.zsh.org/mla/workers/2015/msg01225.html

Yes, we now have tests for the following cases.  It's trivial to add more
if you can think of any that might be problematic.

pws

  test -z \( || print Not zero 1
  test -z \< || print Not zero 2
  test -n \( && print Not zero 3
  test -n \) && print Not zero 4
  [ -n \> ] && print Not zero 5
  [ -n \! ] && print Not zero 6
0:test with two arguments and a token
>Not zero 1
>Not zero 2
>Not zero 3
>Not zero 4
>Not zero 5
>Not zero 6


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [BUG] 'test' returns 1 (not >1) on error [was: test is not posix]
  2016-01-21 12:41   ` Peter Stephenson
@ 2016-01-21 13:06     ` Martijn Dekker
  2016-01-21 16:20       ` Peter Stephenson
  0 siblings, 1 reply; 7+ messages in thread
From: Martijn Dekker @ 2016-01-21 13:06 UTC (permalink / raw)
  To: zsh-workers; +Cc: Peter Stephenson

Peter Stephenson schreef op 21-01-16 om 13:41:
> Yes, we now have tests for the following cases.  It's trivial to add more
> if you can think of any that might be problematic.

Something I also noticed back in May, and which I'm now reminded of, is
that test/[ returns status 1, and not 2 or higher, if an error occurs.

$ [ x = a b ]
[: too many arguments
$ echo $?
1
$ [ 1 -eq ]
zsh: parse error: condition expected: 1
$ echo $?
1

This makes it impossible to test for the failure of test/[ using the
exit status.

POSIX says the status should be 2 or higher on error:

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html#tag_20_128_14

Thanks,

- Martijn


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [BUG] 'test' returns 1 (not >1) on error [was: test is not posix]
  2016-01-21 13:06     ` [BUG] 'test' returns 1 (not >1) on error [was: test is not posix] Martijn Dekker
@ 2016-01-21 16:20       ` Peter Stephenson
  0 siblings, 0 replies; 7+ messages in thread
From: Peter Stephenson @ 2016-01-21 16:20 UTC (permalink / raw)
  To: zsh-workers

On Thu, 21 Jan 2016 14:06:38 +0100
Martijn Dekker <martijn@inlv.org> wrote:
> Something I also noticed back in May, and which I'm now reminded of, is
> that test/[ returns status 1, and not 2 or higher, if an error occurs.

It's correct inside the condition code, so this is yet another thing the
test builtin needs fixing up for.

diff --git a/Src/builtin.c b/Src/builtin.c
index dd20f9e..98ecb09 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -6531,7 +6531,7 @@ bin_test(char *name, char **argv, UNUSED(Options ops), int func)
 	for (s = argv; *s; s++);
 	if (s == argv || strcmp(s[-1], "]")) {
 	    zwarnnam(name, "']' expected");
-	    return 1;
+	    return 2;
 	}
 	s[-1] = NULL;
     }
@@ -6574,19 +6574,19 @@ bin_test(char *name, char **argv, UNUSED(Options ops), int func)
     if (errflag) {
 	errflag &= ~ERRFLAG_ERROR;
 	zcontext_restore();
-	return 1;
+	return 2;
     }
 
     if (!prog || tok == LEXERR) {
 	zwarnnam(name, tokstr ? "parse error" : "argument expected");
 	zcontext_restore();
-	return 1;
+	return 2;
     }
     zcontext_restore();
 
     if (*curtestarg) {
 	zwarnnam(name, "too many arguments");
-	return 1;
+	return 2;
     }
 
     /* syntax is OK, so evaluate */
diff --git a/Test/C02cond.ztst b/Test/C02cond.ztst
index 0b4608a..88cad0d 100644
--- a/Test/C02cond.ztst
+++ b/Test/C02cond.ztst
@@ -223,27 +223,27 @@ F:Failures in these cases do not indicate a problem in the shell.
 0:substitution in `[' builtin
 
   [ -n foo scrimble ]
-1:argument checking for [ builtin
+2:argument checking for [ builtin
 ?(eval):[:1: too many arguments
 
   test -n foo scramble
-1:argument checking for test builtin
+2:argument checking for test builtin
 ?(eval):test:1: too many arguments
 
   [ -n foo scrimble scromble ]
-1:argument checking for [ builtin
+2:argument checking for [ builtin
 ?(eval):[:1: too many arguments
 
   test -n foo scramble scrumble
-1:argument checking for test builtin
+2:argument checking for test builtin
 ?(eval):test:1: too many arguments
 
   [ -n foo -a -n bar scrimble ]
-1:argument checking for [ builtin
+2:argument checking for [ builtin
 ?(eval):[:1: too many arguments
 
   test -n foo -a -z "" scramble
-1:argument checking for test builtin
+2:argument checking for test builtin
 ?(eval):test:1: too many arguments
 
   fn() {


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2016-01-21 16:21 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-20 21:06 test is not posix Trek
2016-01-20 21:38 ` Bart Schaefer
2016-01-20 21:44   ` Trek
2016-01-20 23:46 ` Martijn Dekker
2016-01-21 12:41   ` Peter Stephenson
2016-01-21 13:06     ` [BUG] 'test' returns 1 (not >1) on error [was: test is not posix] Martijn Dekker
2016-01-21 16:20       ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).