Subject: bin/221: test unary operators with no argument always return true
To: None <gnats-admin>
From: None <jarle@idt.unit.no>
List: netbsd-bugs
Date: 04/09/1994 17:20:02
>Number:         221
>Category:       bin
>Synopsis:       /bin/test unary operators without argument always return true
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    gnats-admin (Utility Bug People)
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sat Apr  9 17:20:02 1994
>Originator:     Jarle Greipsland
>Organization:
"Redundancy department of Redundancy"
>Release:        current
>Environment:
Intel 486DX2-66, NetBSD-current, do., misc.
	
System: NetBSD darling.idt.unit.no 0.9a DARLING#16 i386


>Description:
One or more of /bin/test (/bin/[) unary operators return a true value if their
arguments are missing.  I find this behaviour somewhat counter-intuitive.
For all I know this may be the expected behaviour according to some standard, 
but I'm not well enough versed in those to say.  (Heck, I don't know who would 
be the most scared if we met in a dark alley, me or the POSIX.....)
	
>How-To-Repeat:
Running /bin/sh
$ if [ -r ]; then
> echo I can read the file
> fi
I can read the file
$ 

On a SunOS box this gives:
$ if [ -r ]; then
> echo I can read the file
> fi
test: argument expected
$ 

as I would expect.
	
>Fix:
Note: As I know diddly'squat about POSIX this may not be correct, but
still, I think it's better :-)

*** test.c.orig	Sun Apr 10 02:07:35 1994
--- test.c	Sun Apr 10 02:02:34 1994
***************
*** 138,141 ****
--- 138,146 ----
  	case 1:				/* % test arg */
  		/* MIPS machine returns NULL of '[ ]' is called. */
+ 		if ( argv[1] != 0 &&
+ 		     (lookup_op(argv[1], unary_op) != -1 ||
+ 		      lookup_op(argv[1], binary_op) != -1 ||
+ 		      lookup_op(argv[1], andor_op) != -1))
+ 			syntax();
  		return (argv[1] == 0 || *argv[1] == '\0') ? 1 : 0;
  		break;

The resulting output from running "/bin/test -r" will now be
"test: syntax error" (which is a bit more helpful the day one of your 
shell variables suddenly expands to nothing).

					-jarle
----
"The solution implemented here works amazingly well in most
 circumstances, and, being essentially half-assed, is more in keeping
 with the spirit of Unix. Blech."
				-- cmushell.el comment


	
>Audit-Trail:
>Unformatted:


------------------------------------------------------------------------------