NetBSD Problem Report #8893

Received: (qmail 10443 invoked from network); 27 Nov 1999 11:46:02 -0000
Message-Id: <199911271145.LAA04399@mono.org>
Date: Sat, 27 Nov 1999 11:45:54 GMT
From: abs@mono.org
Reply-To: abs@mono.org
To: gnats-bugs@gnats.netbsd.org
Subject: possible parsing bug in test?
X-Send-Pr-Version: 3.95

>Number:         8893
>Category:       bin
>Synopsis:       possible parsing bug in test
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    mycroft
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sat Nov 27 03:48:00 +0000 1999
>Closed-Date:    Tue Jul 06 22:09:16 +0000 2004
>Last-Modified:  Tue Jul 06 22:09:16 +0000 2004
>Originator:     David Brownlee
>Release:        1.4.1
>Organization:

		David/absolute
>Environment:

System: NetBSD oblivion.mono.org 1.4.1 NetBSD 1.4.1 (_OBLIVION_) #2: Sat Oct 23 10:51:48 BST 1999 abs@oblivion:/usr/src/sys/arch/i386/compile/_OBLIVION_ i386


>Description:
	The following breaks with '[: HELP: unknown operand' if $x is '-s'.
	[ "$x" = HELP -o "$x" = BOB ] && echo bob
	While the following works fine
	[ "$x" = HELP ] && echo bob

	I'm assuming that test is interpreting the -s as a test flag.

>How-To-Repeat:
	See above

>Fix:
	If the above breakage is correct, then just about every shell script
	that uses -o or -a with arbitrary string comparison is going to need
	to do something like:
	[ "X$x" = XHELP -o "X$x" = XBOB ] && echo bob
	which is ugly, but if true needs to be documented in the manpage

	For reference, Solaris 2.6 does not have this problem.

>Release-Note:
>Audit-Trail:

From: Hubert Feyrer <feyrer@rfhs8012.fh-regensburg.de>
To: abs@mono.org
Cc: gnats-bugs@gnats.netbsd.org
Subject: Re: bin/8893: possible parsing bug in test?
Date: Sat, 27 Nov 1999 17:13:21 +0100 (MET)

 On Sat, 27 Nov 1999 abs@mono.org wrote:
 > >Description:
 > 	The following breaks with '[: HELP: unknown operand' if $x is '-s'.
 > 	[ "$x" = HELP -o "$x" = BOB ] && echo bob
 > 	While the following works fine
 > 	[ "$x" = HELP ] && echo bob
 > 
 > 	I'm assuming that test is interpreting the -s as a test flag.

 Yes of course. Just enclosing things in quotes doesn't change them. 
 I'd rather say this is a bug in Solaris that this works.


  - Hubert

 -- 
 NetBSD - Better for your uptime than Viagra


From: David Brownlee <abs@mono.org>
To: hubert.feyrer@informatik.fh-regensburg.de
Cc: gnats-bugs@gnats.netbsd.org
Subject: Re: bin/8893: possible parsing bug in test?
Date: Sat, 27 Nov 1999 18:36:45 +0000 (GMT)

 	The quotes are to ensure it does not break if x is ''.
 	The test is unambiguous - if it can work out
 		[ -s = HELP ]
 	it should be able to work out
 		[ -s = HELP -o -s = BOB ]

 	I can understand a simplistic test only getting the first right,
 	but either both should work, or both should break.


 		David/absolute

 On Sat, 27 Nov 1999, Hubert Feyrer wrote:

 > On Sat, 27 Nov 1999 abs@mono.org wrote:
 > > >Description:
 > > 	The following breaks with '[: HELP: unknown operand' if $x is '-s'.
 > > 	[ "$x" = HELP -o "$x" = BOB ] && echo bob
 > > 	While the following works fine
 > > 	[ "$x" = HELP ] && echo bob
 > > 
 > > 	I'm assuming that test is interpreting the -s as a test flag.
 > 
 > Yes of course. Just enclosing things in quotes doesn't change them. 
 > I'd rather say this is a bug in Solaris that this works.
 > 
 > 
 >  - Hubert
 > 
 > -- 
 > NetBSD - Better for your uptime than Viagra
 > 


From: David Brownlee <abs@mono.org>
To: hubert.feyrer@informatik.fh-regensburg.de
Cc: gnats-bugs@gnats.netbsd.org
Subject: Re: bin/8893: possible parsing bug in test?
Date: Mon, 29 Nov 1999 10:46:50 +0000 (GMT)

         Hmm - looking at the manpage for test it should be possible             
         to unambiguously determine when -s is a test and not a variable.        
         All test expressions are of the form:                                   
                 value                                                           
                 -<flag> value                                                   
                 value <test> value                                              
         So simply by the number of arguments the type can be verified.          

         With the addition or -a and -o it becomes more complicated:             
                 [ -s $x -o $y = $z ] when ${x,y,}=-o gives                      
                 [ -s -o -o -o = -o ]                                            

         It is about this point my head explodes, and I decide the best
 	option is probably to update the manpage to indicate the problem
 	exists and the suggested workaround :)

 		David/absolute



From: Giles Lean <giles@nemeton.com.au>
To: abs@mono.org
Cc: gnats-bugs@gnats.netbsd.org
Subject: Re: bin/8893: possible parsing bug in test? 
Date: Fri, 03 Dec 1999 08:39:48 +1100

 On Sat, 27 Nov 1999 11:45:54 GMT  abs@mono.org wrote:

 > 	The following breaks with '[: HELP: unknown operand' if $x is '-s'.
 > 	[ "$x" = HELP -o "$x" = BOB ] && echo bob
 > 	While the following works fine
 > 	[ "$x" = HELP ] && echo bob
 > 
 > 	I'm assuming that test is interpreting the -s as a test flag.
 > 
 > 	For reference, Solaris 2.6 does not have this problem.

 Interesting ... here I get:

 /bin/test	fails
 ksh [ builtin	fails
 sh [ builtin	OK

 (My i've-gotta-upgrade-this 1.4_ALPHA system, not 1.4.1 as you were
 using.)

 From looking at the standard for test(1) I think this behaviour is
 wrong.  The grammar for test is not really good though, and there is
 considerable room for interpretation!

 The interpretation I would _like_ is that

     [ -s = HELP -o -s = BOB ]

 be parsed as

      expression1 -o expression2

 The standard mutters vaguely about "more than four options" and "the
 precedence and associativity defined above" but it does explicitly say
 that:

    "In addition, the string comparison binary primaries "=" and !=
     have a higher precedence than any unary primary."

 So I think you have found a bug.  Does anyone care to tackle fixing
 this, without breaking too much else? :-)

 Regards,

 Giles





From: Todd Whitesel <toddpw@best.com>
To: abs@mono.org
Cc: gnats-bugs@gnats.netbsd.org, netbsd-bugs@netbsd.org
Subject: Re: bin/8893: possible parsing bug in test?
Date: Fri, 3 Dec 1999 01:26:02 -0800 (PST)

 > 	If the above breakage is correct, then just about every shell script
 > 	that uses -o or -a with arbitrary string comparison is going to need
 > 	to do something like:
 > 	[ "X$x" = XHELP -o "X$x" = XBOB ] && echo bob
 > 	which is ugly, but if true needs to be documented in the manpage

 I've been doing this in my scripts for many years, because many versions
 of test (or a shell built-in) aren't smart enough to successfully decode
 all the variant cases and do the right thing in all of them.

 I remember reading somewhere that there was a basic parsing ambiguity in
 the syntax of test(1), and so it would never be possible to totally fix
 this problem. Since there are already plenty of existing broken binaries
 out there, it is just better to code defensively.

 I agree that the man page should have a note added in the BUGS section,
 and perhaps in the main text, pointing out this subtlely of the syntax.

 Todd Whitesel
 toddpw @ best.com

From: Frederick Bruckman <fb@enteract.com>
To: Todd Whitesel <toddpw@best.com>
Cc: abs@mono.org, gnats-bugs@gnats.netbsd.org
Subject: Re: bin/8893: possible parsing bug in test?
Date: Fri, 3 Dec 1999 06:26:15 -0600 (CST)

 On Fri, 3 Dec 1999, Todd Whitesel wrote:

 > > 	If the above breakage is correct, then just about every shell script
 > > 	that uses -o or -a with arbitrary string comparison is going to need
 > > 	to do something like:
 > > 	[ "X$x" = XHELP -o "X$x" = XBOB ] && echo bob
 > > 	which is ugly, but if true needs to be documented in the manpage

 > I agree that the man page should have a note added in the BUGS section,
 > and perhaps in the main text, pointing out this subtlely of the syntax.

 But there is already a section in the test(1) man page titled
 "Grammar Ambiguity". Could that be where you read it?


State-Changed-From-To: open->closed 
State-Changed-By: mycroft 
State-Changed-When: Tue Jul 6 22:07:04 UTC 2004 
State-Changed-Why:  
This was never a bug.  Nevertheless, the behavior was changed a long time ago. 


Responsible-Changed-From-To: bin-bug-people->mycroft 
Responsible-Changed-By: mycroft 
Responsible-Changed-When: Tue Jul 6 22:07:04 UTC 2004 
Responsible-Changed-Why:  
. 
>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.