From arensb@clue.umiacs.umd.edu Sun Jul 25 07:50:42 1999
Return-Path: <arensb@clue.umiacs.umd.edu>
Received: from clue.umiacs.umd.edu (clue.umiacs.umd.edu [128.8.120.175])
	by hub.freebsd.org (Postfix) with ESMTP id 329671516E
	for <FreeBSD-gnats-submit@freebsd.org>; Sun, 25 Jul 1999 07:50:41 -0700 (PDT)
	(envelope-from arensb@clue.umiacs.umd.edu)
Received: (from arensb@localhost)
	by clue.umiacs.umd.edu (8.9.3/8.9.3) id KAA13223;
	Sun, 25 Jul 1999 10:49:36 -0400 (EDT)
	(envelope-from arensb)
Message-Id: <199907251449.KAA13223@clue.umiacs.umd.edu>
Date: Sun, 25 Jul 1999 10:49:36 -0400 (EDT)
From: Andrew Arensburger <arensb@clue.umiacs.umd.edu>
Reply-To: arensb@clue.umiacs.umd.edu
To: FreeBSD-gnats-submit@freebsd.org
Subject: `sh -e' doesn't parse multi-command lines correctly
X-Send-Pr-Version: 3.2

>Number:         12806
>Category:       bin
>Synopsis:       `sh -e' doesn't parse multi-command lines correctly
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sun Jul 25 08:00:00 PDT 1999
>Closed-Date:    Mon Jul 16 02:19:35 PDT 2001
>Last-Modified:  Mon Jul 16 02:21:21 PDT 2001
>Originator:     Andrew Arensburger
>Release:        FreeBSD 3.2-RELEASE i386
>Organization:
University of Maryland Institute for Advanced Computer Studies
>Environment:

        I've found this bug under every version of FreeBSD where I've
tested it:
        FreeBSD 2.2.7-RELEASE i386
        FreeBSD 3.1-RELEASE i386
        FreeBSD 3.2-RELEASE i386

>Description:

        sh(1) says,

:    -e errexit
:            If not interactive, exit immediately if any untested command
:            fails.  The exit status of a command is considered to be explic-
:            itly 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.

This works fine if the script is of the form 
        cmd1
        cmd2 && cmd3
but not if it is of the form
        cmd1; cmd2 && cmd3

        One practical upshot is that if you have a Makefile built with
GNU automake, "make clean" does not work.

>How-To-Repeat:

        The following two scripts should behave identically:

#!/bin/sh -e
echo before
test foo = bar && echo blah
echo after

#!/bin/sh -e
echo before; test foo = bar && echo blah
echo after

        The first one prints

        before
        after

as expected. The second one, however, prints

        before

and exits.

>Fix:

        Don't know. If I come up with one, I'll let you know.

>Release-Note:
>Audit-Trail:

From: "Danny J. Zerkel" <dzerkel@columbus.rr.com>
To: freebsd-gnats-submit@freebsd.org, arensb@clue.umiacs.umd.edu
Cc:  
Subject: Re: bin/12806: `sh -e' doesn't parse multi-command lines correctly
Date: Thu, 05 Aug 1999 23:20:12 -0400

 I confirmed that ksh works as expected and the sh on Solaris works as
 expected,
 so I poked around and I have included a patch below.  What it looks like
 is that
 for a semicolon node, evaltree() calls evaltree() for both sides of the
 node and
 then retests the eflag, even though it has already been tested on each
 side of
 the node.  There is no reason, that I can see, for the semicolon node to
 check
 the eflag, so I just made it do the other tests and return.
 
 --- /usr/src/bin/sh/eval.c.orig Thu Aug  5 23:08:44 1999
 +++ /usr/src/bin/sh/eval.c      Thu Aug  5 23:13:31 1999
 @@ -200,10 +200,14 @@
         switch (n->type) {
         case NSEMI:
                 evaltree(n->nbinary.ch1, 0);
 -               if (evalskip)
 -                       goto out;
 -               evaltree(n->nbinary.ch2, flags);
 -               break;
 +               if (!evalskip) {
 +                       evaltree(n->nbinary.ch2, flags);
 +               }
 +               if (pendingsigs)
 +                       dotrap();
 +               if (flags & EV_EXIT)
 +                       exitshell(exitstatus);
 +               return;
         case NAND:
                 evaltree(n->nbinary.ch1, EV_TESTED);
                 if (evalskip || exitstatus != 0) {
 
 -- Danny J. Zerkel
 dzerkel@columbus.rr.com
 

From: Andrew Arensburger <arensb@cfar.umd.edu>
To: "Danny J. Zerkel" <dzerkel@columbus.rr.com>
Cc: freebsd-gnats-submit@freebsd.org, arensb@cfar.umd.edu
Subject: Re: bin/12806: `sh -e' doesn't parse multi-command lines correctly 
Date: Fri, 06 Aug 1999 10:43:33 -0400

 On Thu, 05 Aug 1999 23:20:12 EDT, "Danny J. Zerkel" wrote:
 > What it looks like
 > is that
 > for a semicolon node, evaltree() calls evaltree() for both sides of the
 > node and
 > then retests the eflag, even though it has already been tested on each
 > side of
 > the node.  There is no reason, that I can see, for the semicolon node to
 > check
 > the eflag, so I just made it do the other tests and return.
 
 	This patch works for me. While it's not as elegant as I would
 have liked (but I tend to be anal), I haven't been able to come up
 with a better solution.
 	Theoretically, this patch might change the behavior of /bin/sh
 if you ever wound up with a tree of the form
 
 	        NAND
 	       /    \
 	   NSEMI    stmt
 	    / \
 	stmt   stmt
 
 but this can't happen, can it?
 
 -- 
 Andrew Arensburger, Systems guy		Center for Automation Research
 arensb@cfar.umd.edu			University of Maryland
      Now we dolly back, now we fade to black... and roll credits!
 

From: "Danny J. Zerkel" <dzerkel@columbus.rr.com>
To: freebsd-gnats-submit@FreeBSD.org, arensb@clue.umiacs.umd.edu
Cc:  
Subject: Re: bin/12806: `sh -e' doesn't parse multi-command lines correctly
Date: Sun, 15 Jul 2001 23:20:45 -0400

 Some other fix has been applied, which also fixes this problem.
 
 This PR can be closed
 
 ------------------
 dzerkel@columbus.rr.com
State-Changed-From-To: open->closed 
State-Changed-By: steve 
State-Changed-When: Mon Jul 16 02:19:35 PDT 2001 
State-Changed-Why:  
As "Danny J. Zerkel" <dzerkel@columbus.rr.com> points out this has been 
fixed.  Thanks. 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=12806 
>Unformatted:
