From mori@tri.asanuma.co.jp  Mon Jul  9 17:30:09 2001
Return-Path: <mori@tri.asanuma.co.jp>
Received: from shiva.tri.asanuma.co.jp (shiva.tri.asanuma.co.jp [210.160.188.2])
	by hub.freebsd.org (Postfix) with ESMTP id 4110F37B403
	for <FreeBSD-gnats-submit@freebsd.org>; Mon,  9 Jul 2001 17:30:08 -0700 (PDT)
	(envelope-from mori@tri.asanuma.co.jp)
Received: from yashoda.tri.asanuma.co.jp (yashoda.tri.asanuma.co.jp [172.16.57.11])
	by shiva.tri.asanuma.co.jp (Postfix) with ESMTP id 98363542B
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 10 Jul 2001 09:30:06 +0900 (JST)
Received: from localhost (kurishna.tri.asanuma.co.jp [172.16.57.2])
	by yashoda.tri.asanuma.co.jp (8.11.0/8.11.0) with ESMTP id f6A0U6x16020;
	Tue, 10 Jul 2001 09:30:06 +0900 (JST)
Message-Id: <20010710.093006.126640528.mori@tri.asanuma.co.jp>
Date: Tue, 10 Jul 2001 09:30:06 +0900 (JST)
From: MORI Kouji <mori@tri.asanuma.co.jp>
To: FreeBSD-gnats-submit@freebsd.org
Subject: behavior of /bin/sh with -e option looks incorrect

>Number:         28852
>Category:       bin
>Synopsis:       behavior of /bin/sh with -e option looks incorrect
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    cracauer
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Jul 09 17:40:01 PDT 2001
>Closed-Date:    Sat Dec 27 21:18:54 EST 2003
>Last-Modified:  Sat Dec 27 21:18:54 EST 2003
>Originator:     
>Release:        FreeBSD 4.3-STABLE i386
>Organization:
Techinical Reserach Institute, Asanuma Corp., Takatsuki, Japan
>Environment:
System: FreeBSD kurishna.tri.asanuma.co.jp 4.3-STABLE FreeBSD 4.3-STABLE #54: Fri Jun 22 11:28:51 JST 2001 mori@kurishna.tri.asanuma.co.jp:/opt/FreeBSD-RELENG_4/obj/opt/FreeBSD-RELENG_4/src/sys/KOTONE i386


>Description:
	In sh(1), about '-e' and 'errexit' is follows description.

     -e errexit
             Exit immediately if any untested command fails in non-interactive
             mode.  The exit status of a command is considered to be explicit-
             ly tested if the command is used to control an if, elif, while,
             or until; or if the command is the left hand operand of an ``&&''
             or ``||'' operator.

	But in 'if' statement, if a left command of '&&' failes then
 	exit immediately.

	This behavior is found doing follows command.  And this is found
	only in FreeBSD and NetBSD.

	$ sh -ecx 'if true; then false && true; fi; true'
	+ true
	+ false
	$

	In bash-2.05,

	$ bash -ecx 'if true; then false && true; fi; true'
	+ true
	+ false
	+ true
	$

	In SunOS 5.7's /bin/sh

	$ /bin/sh -ecx 'if true; then false && true; fi; true'
	+ true 
	+ false 
	+ true 

	In SunOS 5.7's /bin/ksh

	$ /bin/ksh -ecx 'if true; then false && true; fi; true'
	+ true
	+ false
	+ true
	$


>How-To-Repeat:
	Do follow command, please.
	We expect last 'true', but isn't appeared.

	$ sh -ecx 'if true; then false && true; fi; true'
	+ true
	+ false
	$

>Fix:

Index: eval.c
===================================================================
RCS file: /opt/cvs/FreeBSD/src/bin/sh/eval.c,v
retrieving revision 1.27.2.3
diff -u -r1.27.2.3 eval.c
--- eval.c	2001/07/05 00:41:14	1.27.2.3
+++ eval.c	2001/07/09 23:55:18
@@ -208,7 +208,6 @@
 	case NAND:
 		evaltree(n->nbinary.ch1, EV_TESTED);
 		if (evalskip || exitstatus != 0) {
-			flags |= EV_TESTED;
 			goto out;
 		}
 		evaltree(n->nbinary.ch2, flags);
@@ -249,25 +248,9 @@
 		break;
 	case NFOR:
 		evalfor(n);
-		/*
-		 * The 'for' command does not set exitstatus, so the value
-		 * now in exitstatus is from the last command executed in
-		 * the 'for' loop.  That exit value had been tested (wrt
-		 * 'sh -e' checking) while processing that command, and
-		 * it should not be re-tested here.
-		 */
-		flags |= EV_TESTED;
 		break;
 	case NCASE:
 		evalcase(n, flags);
-		/*
-		 * The 'case' command does not set exitstatus, so the value
-		 * now in exitstatus is from the last command executed in
-		 * the 'case' block.  That exit value had been tested (wrt
-		 * 'sh -e' checking) while processing that command, and
-		 * it should not be re-tested here.
-		 */
-		flags |= EV_TESTED;
 		break;
 	case NDEFUN:
 		defun(n->narg.text, n->narg.next);
@@ -292,14 +275,8 @@
 out:
 	if (pendingsigs)
 		dotrap();
-	/*
-	 * XXX - Like "!(n->type == NSEMI)", more types will probably
-	 * need to be excluded from this test. It's probably better
-	 * to set or unset EV_TESTED in the loop above than to bloat
-	 * the conditional here.
-	 */
 	if ((flags & EV_EXIT) || (eflag && exitstatus 
-	    && !(flags & EV_TESTED) && !(n->type == NSEMI)))
+	    && !(flags & EV_TESTED) && (n->type == NCMD)))
 		exitshell(exitstatus);
 }
 
>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->chaucer 
Responsible-Changed-By: dwmalone 
Responsible-Changed-When: Tue Jul 10 02:41:18 PDT 2001 
Responsible-Changed-Why:  
Martin has an interest in sh. 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=28852 
Responsible-Changed-From-To: chaucer->cracauer 
Responsible-Changed-By: dwmalone 
Responsible-Changed-When: Tue Jul 10 02:52:53 PDT 2001 
Responsible-Changed-Why:  
Spell Martin's name correctly... Martin - this PR contains a sh patch 
and ex 
http://www.FreeBSD.org/cgi/query-pr.cgi?pr=28852 
State-Changed-From-To: open->closed 
State-Changed-By: cracauer 
State-Changed-When: Sat Dec 27 21:18:01 EST 2003 
State-Changed-Why:  
Fixed in the HEAD. 

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