From nobody@FreeBSD.org  Thu Jan  8 12:21:06 2009
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 6CDF81065672
	for <freebsd-gnats-submit@FreeBSD.org>; Thu,  8 Jan 2009 12:21:06 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21])
	by mx1.freebsd.org (Postfix) with ESMTP id 5B0038FC13
	for <freebsd-gnats-submit@FreeBSD.org>; Thu,  8 Jan 2009 12:21:06 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.3/8.14.3) with ESMTP id n08CL5m5094808
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 8 Jan 2009 12:21:05 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.3/8.14.3/Submit) id n08CL5J9094796;
	Thu, 8 Jan 2009 12:21:05 GMT
	(envelope-from nobody)
Message-Id: <200901081221.n08CL5J9094796@www.freebsd.org>
Date: Thu, 8 Jan 2009 12:21:05 GMT
From: Martin Beran <mb@tns.cz>
To: freebsd-gnats-submit@FreeBSD.org
Subject: /bin/sh does not handle negation correctly in complex commands
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         130298
>Category:       bin
>Synopsis:       sh(1) does not handle negation correctly in complex commands
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    stefanf
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu Jan 08 12:30:01 UTC 2009
>Closed-Date:    Thu Oct 08 17:12:41 UTC 2009
>Last-Modified:  Thu Oct 08 17:12:41 UTC 2009
>Originator:     Martin Beran
>Release:        7.0-RELEASE-p6
>Organization:
Trusted Network Solutions, a. s.
>Environment:
FreeBSD mb.tns.cz 7.0-RELEASE-p6 FreeBSD 7.0-RELEASE-p6 #2: Wed Nov 26 09:21:51 CET 2008     root@mb.tns.cz:/usr/obj/usr/src/sys/MB  i386
>Description:
Negation of a pipeline exit status (the shell construct ! cmd1 | cmd2) does not work when joined with another command by &&: cmd3 && ! cmd1 | cmd2. The exit status is the same as in: cmd3 && cmd1 | cmd2. Using braces triggers the correct behavior: cmd3 && { ! cmd1 | cmd2; }.
>How-To-Repeat:
true && ! true | true; echo $?
This should display 1 (as it does, for example, in bash), but displays 0.
>Fix:
true && ! true | true; echo $?

>Release-Note:
>Audit-Trail:

From: Jilles Tjoelker <jilles@stack.nl>
To: bug-followup@FreeBSD.org, mb@tns.cz, stefanf@freebsd.org
Cc:  
Subject: Re: bin/130298: sh(1) does not handle negation correctly in
	complex commands
Date: Sun, 5 Apr 2009 00:28:14 +0200

 --C7zPtVaVf+AK4Oqc
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: inline
 
 This is a subtle bug in the parser. It keeps track of whether it should
 recognize keywords such as '!' and 'if' (checkkwd global), but it fails
 to enable this recognition again after && and ||. Hence, the '!' is not
 recognized by pipeline(). Then, command() does enable the recognition
 and picks up the '!'. So,  cmd1 && ! cmd2 | cmd3  puts the negation on
 cmd2 instead of cmd2 | cmd3 and does not work properly. Setting checkkwd
 in pipeline() fixes the problem (patch attached).
 
 After I figured this out I noticed NetBSD had already done it, and in
 the same way.
 http://cvsweb.netbsd.org/bsdweb.cgi/src/bin/sh/parser.c.diff?r1=1.64&r2=1.65&f=h
 
 If the attachment is mangled, try
 http://www.stack.nl/~jilles/unix/sh-parser-and-pipe-not.patch
 
 -- 
 Jilles Tjoelker
 
 --C7zPtVaVf+AK4Oqc
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: attachment; filename="sh-parser-and-pipe-not.patch.txt"
 
 --- src/bin/sh/parser.c.orig	2008-09-04 19:34:53.000000000 +0200
 +++ src/bin/sh/parser.c	2009-04-05 00:02:51.000000000 +0200
 @@ -250,6 +250,7 @@ pipeline(void)
  	int negate;
  
  	negate = 0;
 +	checkkwd = 2;
  	TRACE(("pipeline: entered\n"));
  	while (readtoken() == TNOT)
  		negate = !negate;
 
 --C7zPtVaVf+AK4Oqc--
Responsible-Changed-From-To: freebsd-bugs->stefanf 
Responsible-Changed-By: stefanf 
Responsible-Changed-When: Mon Apr 6 21:49:20 UTC 2009 
Responsible-Changed-Why:  
I'll handle this. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=130298 
State-Changed-From-To: open->patched 
State-Changed-By: stefanf 
State-Changed-When: Mon Apr 13 19:12:35 UTC 2009 
State-Changed-Why:  
Fixed in current. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=130298 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/130298: commit references a PR
Date: Mon, 13 Apr 2009 19:36:56 +0000 (UTC)

 Author: stefanf
 Date: Mon Apr 13 19:10:56 2009
 New Revision: 191009
 URL: http://svn.freebsd.org/changeset/base/191009
 
 Log:
   Parse 'cmd1 && ! cmd2 | cmd3' correctly, the bang should apply to the entire
   pipeline cmd2 | cmd3 and not just cmd2.
   
   PR:		130298
   Submitted by:	Jilles Tjoelker
 
 Modified:
   head/bin/sh/parser.c
 
 Modified: head/bin/sh/parser.c
 ==============================================================================
 --- head/bin/sh/parser.c	Mon Apr 13 18:56:53 2009	(r191008)
 +++ head/bin/sh/parser.c	Mon Apr 13 19:10:56 2009	(r191009)
 @@ -250,6 +250,7 @@ pipeline(void)
  	int negate;
  
  	negate = 0;
 +	checkkwd = 2;
  	TRACE(("pipeline: entered\n"));
  	while (readtoken() == TNOT)
  		negate = !negate;
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: patched->closed 
State-Changed-By: stefanf 
State-Changed-When: Thu Oct 8 17:12:08 UTC 2009 
State-Changed-Why:  
Merged to stable/7. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=130298 
>Unformatted:
