From nobody@FreeBSD.org  Sat May 23 20:41:00 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 C946A1065674
	for <freebsd-gnats-submit@FreeBSD.org>; Sat, 23 May 2009 20:41:00 +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 B7BEC8FC1E
	for <freebsd-gnats-submit@FreeBSD.org>; Sat, 23 May 2009 20:41:00 +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 n4NKf0DD060979
	for <freebsd-gnats-submit@FreeBSD.org>; Sat, 23 May 2009 20:41:00 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.3/8.14.3/Submit) id n4NKf00a060978;
	Sat, 23 May 2009 20:41:00 GMT
	(envelope-from nobody)
Message-Id: <200905232041.n4NKf00a060978@www.freebsd.org>
Date: Sat, 23 May 2009 20:41:00 GMT
From: Mikolaj Golub <to.my.trociny@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: sh(1) with 'set -e' exits immediately in tested eval command
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         134881
>Category:       bin
>Synopsis:       sh(1) with 'set -e' exits immediately in tested eval command
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    stefanf
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sat May 23 20:50:01 UTC 2009
>Closed-Date:    Sun May 16 10:24:55 UTC 2010
>Last-Modified:  Sun May 16 10:24:55 UTC 2010
>Originator:     Mikolaj Golub
>Release:        current
>Organization:
>Environment:
FreeBSD fbsd.zhuzha.ua1 8.0-CURRENT FreeBSD 8.0-CURRENT #0 r191131M: Sat May  9 10:48:19 EEST 2009     root@zhuzha.ua1:/home/golub/freebsd/build/obj/home/golub/freebsd/src/sys/NOSWAPPING  i386

FreeBSD zhuzha.ua1 7.2-PRERELEASE FreeBSD 7.2-PRERELEASE #8: Sun Apr 26 12:08:58 EEST 2009     root@zhuzha.ua1:/usr/obj/usr/src/sys/DEBUG  i386

FreeBSD kopusha.onet 6.4-STABLE FreeBSD 6.4-STABLE #0: Tue May  5 03:04:10 EEST 2009     root@kopusha.onet:/usr/obj/usr/src/sys/GENERIC  i386
>Description:
Aleksey Cheusov posted the following sctipt to fido7.ru.unix.bsd, which shows bug in sh(1):

    #!/bin/sh

    set -e

    if eval false; then
        echo 'Really?'
    fi

    echo "Your shell is good"

>How-To-Repeat:

>Fix:


>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->stefanf 
Responsible-Changed-By: stefanf 
Responsible-Changed-When: Sun May 24 10:05:45 UTC 2009 
Responsible-Changed-Why:  
I'll handle this. 

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

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/134881: commit references a PR
Date: Sun, 31 May 2009 12:36:28 +0000 (UTC)

 Author: stefanf
 Date: Sun May 31 12:36:14 2009
 New Revision: 193169
 URL: http://svn.freebsd.org/changeset/base/193169
 
 Log:
   Fix the eval command in combination with set -e.  Before this change the shell
   would always terminate if eval returned with a non-zero exit status regardless
   if the status was actually tested.  Unfortunately a new file-scope variable
   is needed, the alternative would only be to add a new parameter to all
   built-ins.
   
   PR:	134881
 
 Modified:
   head/bin/sh/eval.c
   head/bin/sh/eval.h
   head/bin/sh/histedit.c
   head/bin/sh/main.c
   head/bin/sh/trap.c
 
 Modified: head/bin/sh/eval.c
 ==============================================================================
 --- head/bin/sh/eval.c	Sun May 31 12:16:31 2009	(r193168)
 +++ head/bin/sh/eval.c	Sun May 31 12:36:14 2009	(r193169)
 @@ -83,6 +83,7 @@ MKINIT int evalskip;		/* set if we are s
  STATIC int skipcount;		/* number of levels to skip */
  MKINIT int loopnest;		/* current loop nesting level */
  int funcnest;			/* depth of function calls */
 +STATIC int builtin_flags;	/* evalcommand flags for builtins */
  
  
  char *commandname;
 @@ -147,7 +148,7 @@ evalcmd(int argc, char **argv)
                          STPUTC('\0', concat);
                          p = grabstackstr(concat);
                  }
 -                evalstring(p);
 +                evalstring(p, builtin_flags & EV_TESTED);
          }
          return exitstatus;
  }
 @@ -158,7 +159,7 @@ evalcmd(int argc, char **argv)
   */
  
  void
 -evalstring(char *s)
 +evalstring(char *s, int flags)
  {
  	union node *n;
  	struct stackmark smark;
 @@ -167,7 +168,7 @@ evalstring(char *s)
  	setinputstring(s, 1);
  	while ((n = parsecmd(0)) != NEOF) {
  		if (n != NULL)
 -			evaltree(n, 0);
 +			evaltree(n, flags);
  		popstackmark(&smark);
  	}
  	popfile();
 @@ -839,6 +840,7 @@ evalcommand(union node *cmd, int flags, 
  		commandname = argv[0];
  		argptr = argv + 1;
  		optptr = NULL;			/* initialize nextopt */
 +		builtin_flags = flags;
  		exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
  		flushall();
  cmddone:
 
 Modified: head/bin/sh/eval.h
 ==============================================================================
 --- head/bin/sh/eval.h	Sun May 31 12:16:31 2009	(r193168)
 +++ head/bin/sh/eval.h	Sun May 31 12:36:14 2009	(r193169)
 @@ -46,7 +46,7 @@ struct backcmd {		/* result of evalbackc
  };
  
  int evalcmd(int, char **);
 -void evalstring(char *);
 +void evalstring(char *, int);
  union node;	/* BLETCH for ansi C */
  void evaltree(union node *, int);
  void evalbackcmd(union node *, struct backcmd *);
 
 Modified: head/bin/sh/histedit.c
 ==============================================================================
 --- head/bin/sh/histedit.c	Sun May 31 12:16:31 2009	(r193168)
 +++ head/bin/sh/histedit.c	Sun May 31 12:36:14 2009	(r193169)
 @@ -350,7 +350,7 @@ histcmd(int argc, char **argv)
  				if (displayhist) {
  					out2str(s);
  				}
 -				evalstring(s);
 +				evalstring(s, 0);
  				if (displayhist && hist) {
  					/*
  					 *  XXX what about recursive and
 @@ -382,7 +382,7 @@ histcmd(int argc, char **argv)
  		fclose(efp);
  		editcmd = stalloc(strlen(editor) + strlen(editfile) + 2);
  		sprintf(editcmd, "%s %s", editor, editfile);
 -		evalstring(editcmd);	/* XXX - should use no JC command */
 +		evalstring(editcmd, 0);	/* XXX - should use no JC command */
  		INTON;
  		readcmdfile(editfile);	/* XXX - should read back - quick tst */
  		unlink(editfile);
 
 Modified: head/bin/sh/main.c
 ==============================================================================
 --- head/bin/sh/main.c	Sun May 31 12:16:31 2009	(r193168)
 +++ head/bin/sh/main.c	Sun May 31 12:36:14 2009	(r193169)
 @@ -178,7 +178,7 @@ state2:
  state3:
  	state = 4;
  	if (minusc) {
 -		evalstring(minusc);
 +		evalstring(minusc, 0);
  	}
  	if (sflag || minusc == NULL) {
  state4:	/* XXX ??? - why isn't this before the "if" statement */
 
 Modified: head/bin/sh/trap.c
 ==============================================================================
 --- head/bin/sh/trap.c	Sun May 31 12:16:31 2009	(r193168)
 +++ head/bin/sh/trap.c	Sun May 31 12:36:14 2009	(r193169)
 @@ -416,7 +416,7 @@ dotrap(void)
  					if (i == SIGCHLD)
  						ignore_sigchld++;
  					savestatus = exitstatus;
 -					evalstring(trap[i]);
 +					evalstring(trap[i], 0);
  					exitstatus = savestatus;
  					if (i == SIGCHLD)
  						ignore_sigchld--;
 @@ -471,7 +471,7 @@ exitshell(int status)
  	handler = &loc1;
  	if ((p = trap[0]) != NULL && *p != '\0') {
  		trap[0] = NULL;
 -		evalstring(p);
 +		evalstring(p, 0);
  	}
  l1:   handler = &loc2;			/* probably unnecessary */
  	flushall();
 _______________________________________________
 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: open->patched 
State-Changed-By: stefanf 
State-Changed-When: Mon Jun 1 18:08:11 UTC 2009 
State-Changed-Why:  
Fixed in current. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=134881 
State-Changed-From-To: patched->closed 
State-Changed-By: stefanf 
State-Changed-When: Sun May 16 10:24:33 UTC 2010 
State-Changed-Why:  
Merged back into 7. 

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