NetBSD Problem Report #9194

Received: (qmail 23631 invoked from network); 15 Jan 2000 12:03:59 -0000
Message-Id: <200001151204.VAA21853@serein.jaist.ac.jp>
Date: Sat, 15 Jan 2000 21:04:44 +0900 (JST)
From: akr@jaist.ac.jp
Reply-To: akr@jaist.ac.jp
To: gnats-bugs@gnats.netbsd.org
Subject: /bin/sh -e may abort at a conditional in a loop
X-Send-Pr-Version: 3.95

>Number:         9194
>Category:       bin
>Synopsis:       /bin/sh -e may abort at a conditional in a loop
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    bin-bug-people
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sat Jan 15 04:06:01 +0000 2000
>Closed-Date:    Thu Jan 27 23:40:31 +0000 2000
>Last-Modified:  Thu Jan 27 23:40:46 +0000 2000
>Originator:     Tanaka Akira
>Release:        1.4.1
>Organization:

>Environment:

System: NetBSD serein 1.4.1 NetBSD 1.4.1 (AKR) #3: Sun Sep 19 11:43:39 JST 1999 akr@localhost:/usr/src/sys/arch/i386/compile/AKR i386


>Description:
`/bin/sh -e' exits with conditional in loop.


>How-To-Repeat:


Following script
The script follows should print `succ', but it prints nothing.

#!/bin/sh -e

for i in 0; do
  false && true
done
echo succ

>Fix:

>Release-Note:
>Audit-Trail:

From: Aymeric Vincent <vincent@crans.ens-cachan.fr>
To: akr@jaist.ac.jp
Cc: gnats-bugs@gnats.netbsd.org
Subject: Re: bin/9194: /bin/sh -e may abort at a conditional in a loop
Date: 15 Jan 2000 13:48:35 +0100

 akr@jaist.ac.jp writes:

 > Following script
 > The script follows should print `succ', but it prints nothing.

 No, I don't think it should, because the exit status of the `for'
 loop is the last executed command of its body, which in this case is
 false.

 The whole loop runs OK, but the shell fails when the `for' ends.

 Try this one:
 > #!/bin/sh -e
 > 
 > for i in 0; do
 >   false && true
 > done && true
        ^^^^^^^
 > echo succ

 And you'll get `succ'ess :)

 I don't know what the desired behaviour is, but it looks consistent to
 me...

  Aymeric

From: Martin Cracauer <cracauer@cons.org>
To: akr@jaist.ac.jp
Cc: gnats-bugs@gnats.netbsd.org
Subject: Re: bin/9194: /bin/sh -e may abort at a conditional in a loop
Date: Sat, 15 Jan 2000 14:04:04 +0100

 In <200001151204.VAA21853@serein.jaist.ac.jp>, akr@jaist.ac.jp wrote: 
 > 
 > >Number:         9194
 > >Category:       bin
 > >Synopsis:       /bin/sh -e may abort at a conditional in a loop
 > >Confidential:   no
 > >Severity:       serious
 > >Priority:       medium
 > >Responsible:    bin-bug-people
 > >State:          open
 > >Class:          sw-bug
 > >Submitter-Id:   net
 > >Arrival-Date:   Sat Jan 15 04:06:01 2000
 > >Last-Modified:
 > >Originator:     Tanaka Akira
 > >Organization:
 > 	
 > >Release:        1.4.1
 > >Environment:
 > 	
 > System: NetBSD serein 1.4.1 NetBSD 1.4.1 (AKR) #3: Sun Sep 19 11:43:39 JST 1999 akr@localhost:/usr/src/sys/arch/i386/compile/AKR i386
 > 
 > 
 > >Description:
 > `/bin/sh -e' exits with conditional in loop.
 > 
 > 	
 > >How-To-Repeat:
 > 	
 > 
 > Following script
 > The script follows should print `succ', but it prints nothing.
 > 
 > #!/bin/sh -e
 > 
 > for i in 0; do
 >   false && true
 > done
 > echo succ

 I haven't tested against the NetBSD tree, but I beleive that this
 problem was fixed by the following patch in FreeBSD:

 Index: eval.c
 ===================================================================
 RCS file: /home/CVS-FreeBSD/src/bin/sh/eval.c,v
 retrieving revision 1.13
 retrieving revision 1.14
 diff -c -r1.13 -r1.14
 *** eval.c	1997/05/19 00:18:36	1.13
 --- eval.c	1998/05/04 07:24:10	1.14
 ***************
 *** 764,770 ****
   		for (sp = varlist.list ; sp ; sp = sp->next)
   			mklocal(sp->text);
   		funcnest++;
 ! 		evaltree(cmdentry.u.func, 0);
   		funcnest--;
   		INTOFF;
   		poplocalvars();
 --- 764,773 ----
   		for (sp = varlist.list ; sp ; sp = sp->next)
   			mklocal(sp->text);
   		funcnest++;
 ! 		if (flags & EV_TESTED)
 ! 			evaltree(cmdentry.u.func, EV_TESTED);
 ! 		else
 ! 			evaltree(cmdentry.u.func, 0);
   		funcnest--;
   		INTOFF;
   		poplocalvars();


 -- 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 Martin Cracauer <cracauer@cons.org> http://www.cons.org/cracauer/
   Tel.: (private) +4940 5221829 Fax.: (private) +4940 5228536

From: Tanaka Akira <akr@jaist.ac.jp>
To: Aymeric Vincent <vincent@crans.ens-cachan.fr>,
  Martin Cracauer <cracauer@cons.org>
Cc: gnats-bugs@gnats.netbsd.org
Subject: Re: bin/9194: /bin/sh -e may abort at a conditional in a loop
Date: 19 Jan 2000 06:01:41 +0900

 In article <x7901rec0s.fsf@ungnu.meka7.de>,
   Aymeric Vincent <vincent@crans.ens-cachan.fr> writes:

 > No, I don't think it should, because the exit status of the `for'
 > loop is the last executed command of its body, which in this case is
 > false.

 Hm.  I don't think so because most Bourne shell other than ash
 (SunOS, IRIX, ...) doesn't behave so.  Also, although the exit status
 `for' is non-zero, it is inherited from `false' which is placed on
 left hand operand of && and it's not fatal.  I think these inherited
 non-fatal failure should not treated as an fatal failure.

 In article <20000115140403.A27757@cons.org>,
   Martin Cracauer <cracauer@cons.org> writes:

 > I haven't tested against the NetBSD tree, but I beleive that this
 > problem was fixed by the following patch in FreeBSD:

 Unfortunately, this doesn't fix my problem.
 The script in http://www.jp.freebsd.org/cgi/query-pr.cgi?pr=6047 works
 well, though.

 Also note that I found that ash-linux package on Debian GNU/Linux
 works well with my script.
 http://www.debian.org/Packages/unstable/shells/ash.html
 -- 
 Tanaka Akira

From: Martin Husemann <martin@rumolt.teuto.de>
To: gnats-bugs@gnats.netbsd.org, akr@jaist.ac.jp
Cc:  Subject: RE: bin/9194: /bin/sh -e may abort at a conditional in a loop
Date: Sun, 23 Jan 2000 13:44:00 +0100 (MET)

 Here is a patch that fixes this problem and also PRs bin/9265 and bin/9266.

 The Debian port of ash had all this fixed. They also made some effort to 
 change "char *" to "const char *" and got some minor performance improvements
 from this (avoiding things like strdup) - maybe someone should have a second
 look at it, it's at http://www.debian.org/Packages/unstable/shells/ash.html.


 Martin

 diff -c3 -r sh-orig/eval.c sh/eval.c
 *** sh-orig/eval.c	Wed Oct 13 13:02:10 1999
 --- sh/eval.c	Sun Jan 23 13:35:02 2000
 ***************
 *** 93,100 ****
   int oexitstatus;		/* saved exit status */


 ! STATIC void evalloop __P((union node *));
 ! STATIC void evalfor __P((union node *));
   STATIC void evalcase __P((union node *, int));
   STATIC void evalsubshell __P((union node *, int));
   STATIC void expredir __P((union node *));
 --- 93,100 ----
   int oexitstatus;		/* saved exit status */


 ! STATIC void evalloop __P((union node *, int));
 ! STATIC void evalfor __P((union node *, int));
   STATIC void evalcase __P((union node *, int));
   STATIC void evalsubshell __P((union node *, int));
   STATIC void expredir __P((union node *));
 ***************
 *** 151,157 ****
                           STPUTC('\0', concat);
                           p = grabstackstr(concat);
                   }
 !                 evalstring(p);
           }
           return exitstatus;
   }
 --- 151,157 ----
                           STPUTC('\0', concat);
                           p = grabstackstr(concat);
                   }
 !                 evalstring(p, EV_TESTED);
           }
           return exitstatus;
   }
 ***************
 *** 162,169 ****
    */

   void
 ! evalstring(s)
   	char *s;
   	{
   	union node *n;
   	struct stackmark smark;
 --- 162,170 ----
    */

   void
 ! evalstring(s, flag)
   	char *s;
 + 	int flag;
   	{
   	union node *n;
   	struct stackmark smark;
 ***************
 *** 171,177 ****
   	setstackmark(&smark);
   	setinputstring(s, 1);
   	while ((n = parsecmd(0)) != NEOF) {
 ! 		evaltree(n, 0);
   		popstackmark(&smark);
   	}
   	popfile();
 --- 172,178 ----
   	setstackmark(&smark);
   	setinputstring(s, 1);
   	while ((n = parsecmd(0)) != NEOF) {
 ! 		evaltree(n, flag);
   		popstackmark(&smark);
   	}
   	popfile();
 ***************
 *** 201,207 ****
   	TRACE(("evaltree(0x%lx: %d) called\n", (long)n, n->type));
   	switch (n->type) {
   	case NSEMI:
 ! 		evaltree(n->nbinary.ch1, 0);
   		if (evalskip)
   			goto out;
   		evaltree(n->nbinary.ch2, flags);
 --- 202,208 ----
   	TRACE(("evaltree(0x%lx: %d) called\n", (long)n, n->type));
   	switch (n->type) {
   	case NSEMI:
 ! 		evaltree(n->nbinary.ch1, flags & EV_TESTED);
   		if (evalskip)
   			goto out;
   		evaltree(n->nbinary.ch2, flags);
 ***************
 *** 213,225 ****
   			flags |= EV_TESTED;
   			goto out;
   		}
 ! 		evaltree(n->nbinary.ch2, flags);
   		break;
   	case NOR:
   		evaltree(n->nbinary.ch1, EV_TESTED);
   		if (evalskip || exitstatus == 0)
   			goto out;
 ! 		evaltree(n->nbinary.ch2, flags);
   		break;
   	case NREDIR:
   		expredir(n->nredir.redirect);
 --- 214,226 ----
   			flags |= EV_TESTED;
   			goto out;
   		}
 ! 		evaltree(n->nbinary.ch2, flags | EV_TESTED);
   		break;
   	case NOR:
   		evaltree(n->nbinary.ch1, EV_TESTED);
   		if (evalskip || exitstatus == 0)
   			goto out;
 ! 		evaltree(n->nbinary.ch2, flags | EV_TESTED);
   		break;
   	case NREDIR:
   		expredir(n->nredir.redirect);
 ***************
 *** 247,256 ****
   	}
   	case NWHILE:
   	case NUNTIL:
 ! 		evalloop(n);
   		break;
   	case NFOR:
 ! 		evalfor(n);
   		break;
   	case NCASE:
   		evalcase(n, flags);
 --- 248,257 ----
   	}
   	case NWHILE:
   	case NUNTIL:
 ! 		evalloop(n, flags);
   		break;
   	case NFOR:
 ! 		evalfor(n, flags);
   		break;
   	case NCASE:
   		evalcase(n, flags);
 ***************
 *** 278,291 ****
   out:
   	if (pendingsigs)
   		dotrap();
 ! 	if ((flags & EV_EXIT) || (eflag && exitstatus && !(flags & EV_TESTED)))
   		exitshell(exitstatus);
   }


   STATIC void
 ! evalloop(n)
   	union node *n;
   {
   	int status;

 --- 279,293 ----
   out:
   	if (pendingsigs)
   		dotrap();
 ! 	if (flags & EV_EXIT)
   		exitshell(exitstatus);
   }


   STATIC void
 ! evalloop(n, flags)
   	union node *n;
 + 	int flags;
   {
   	int status;

 ***************
 *** 309,315 ****
   			if (exitstatus == 0)
   				break;
   		}
 ! 		evaltree(n->nbinary.ch2, 0);
   		status = exitstatus;
   		if (evalskip)
   			goto skipping;
 --- 311,317 ----
   			if (exitstatus == 0)
   				break;
   		}
 ! 		evaltree(n->nbinary.ch2, flags & EV_TESTED);
   		status = exitstatus;
   		if (evalskip)
   			goto skipping;
 ***************
 *** 321,328 ****


   STATIC void
 ! evalfor(n)
       union node *n;
   {
   	struct arglist arglist;
   	union node *argp;
 --- 323,331 ----


   STATIC void
 ! evalfor(n, flags)
       union node *n;
 +     int flags;
   {
   	struct arglist arglist;
   	union node *argp;
 ***************
 *** 343,349 ****
   	loopnest++;
   	for (sp = arglist.list ; sp ; sp = sp->next) {
   		setvar(n->nfor.var, sp->text, 0);
 ! 		evaltree(n->nfor.body, 0);
   		if (evalskip) {
   			if (evalskip == SKIPCONT && --skipcount <= 0) {
   				evalskip = 0;
 --- 346,352 ----
   	loopnest++;
   	for (sp = arglist.list ; sp ; sp = sp->next) {
   		setvar(n->nfor.var, sp->text, 0);
 ! 		evaltree(n->nfor.body, flags & EV_TESTED);
   		if (evalskip) {
   			if (evalskip == SKIPCONT && --skipcount <= 0) {
   				evalskip = 0;
 ***************
 *** 571,576 ****
 --- 574,580 ----
   				copyfd(pip[1], 1);
   				close(pip[1]);
   			}
 + 			eflag = 0;
   			evaltree(n, EV_EXIT);
   		}
   		close(pip[1]);
 ***************
 *** 781,787 ****
   		for (sp = varlist.list ; sp ; sp = sp->next)
   			mklocal(sp->text);
   		funcnest++;
 ! 		evaltree(cmdentry.u.func, 0);
   		funcnest--;
   		INTOFF;
   		poplocalvars();
 --- 785,791 ----
   		for (sp = varlist.list ; sp ; sp = sp->next)
   			mklocal(sp->text);
   		funcnest++;
 ! 		evaltree(cmdentry.u.func, flags & EV_TESTED);
   		funcnest--;
   		INTOFF;
   		poplocalvars();
 ***************
 *** 882,887 ****
 --- 886,894 ----
   	if (lastarg)
   		setvar("_", lastarg, 0);
   	popstackmark(&smark);
 + 
 + 	if (eflag && exitstatus && !(flags & EV_TESTED))
 + 		exitshell(exitstatus);
   }


 diff -c3 -r sh-orig/eval.h sh/eval.h
 *** sh-orig/eval.h	Sat Oct 14 00:44:00 1995
 --- sh/eval.h	Sun Jan 23 12:32:31 2000
 ***************
 *** 51,57 ****
   };

   int evalcmd __P((int, char **));
 ! void evalstring __P((char *));
   union node;	/* BLETCH for ansi C */
   void evaltree __P((union node *, int));
   void evalbackcmd __P((union node *, struct backcmd *));
 --- 51,57 ----
   };

   int evalcmd __P((int, char **));
 ! void evalstring __P((char *, int));
   union node;	/* BLETCH for ansi C */
   void evaltree __P((union node *, int));
   void evalbackcmd __P((union node *, struct backcmd *));
 diff -c3 -r sh-orig/histedit.c sh/histedit.c
 *** sh-orig/histedit.c	Fri Jul  9 13:02:06 1999
 --- sh/histedit.c	Sun Jan 23 12:38:55 2000
 ***************
 *** 375,381 ****
   					out2str(s);
   				}

 ! 				evalstring(strcpy(stalloc(strlen(s) + 1), s));
   				free(sp);
   				if (displayhist && hist) {
   					/*
 --- 375,381 ----
   					out2str(s);
   				}

 ! 				evalstring(strcpy(stalloc(strlen(s) + 1), s), 0);
   				free(sp);
   				if (displayhist && hist) {
   					/*
 ***************
 *** 400,406 ****
   		fclose(efp);
   		editcmd = stalloc(strlen(editor) + strlen(editfile) + 2);
   		sprintf(editcmd, "%s %s", editor, editfile);
 ! 		evalstring(editcmd);	/* XXX - should use no JC command */
   		INTON;
   		readcmdfile(editfile);	/* XXX - should read back - quick tst */
   		unlink(editfile);
 --- 400,406 ----
   		fclose(efp);
   		editcmd = stalloc(strlen(editor) + strlen(editfile) + 2);
   		sprintf(editcmd, "%s %s", editor, editfile);
 ! 		evalstring(editcmd, 0);	/* XXX - should use no JC command */
   		INTON;
   		readcmdfile(editfile);	/* XXX - should read back - quick tst */
   		unlink(editfile);
 diff -c3 -r sh-orig/main.c sh/main.c
 *** sh-orig/main.c	Fri Jul  9 13:02:07 1999
 --- sh/main.c	Sun Jan 23 12:39:24 2000
 ***************
 *** 209,215 ****
   	}

   	if (minusc)
 ! 		evalstring(minusc);

   	if (sflag || minusc == NULL) {
   state4:	/* XXX ??? - why isn't this before the "if" statement */
 --- 209,215 ----
   	}

   	if (minusc)
 ! 		evalstring(minusc, 0);

   	if (sflag || minusc == NULL) {
   state4:	/* XXX ??? - why isn't this before the "if" statement */
 diff -c3 -r sh-orig/parser.c sh/parser.c
 *** sh-orig/parser.c	Fri Jul  9 13:02:08 1999
 --- sh/parser.c	Sun Jan 23 13:34:40 2000
 ***************
 *** 245,254 ****

   STATIC union node *
   pipeline() {
 ! 	union node *n1, *pipenode;
   	struct nodelist *lp, *prev;

   	TRACE(("pipeline: entered\n"));
   	n1 = command();
   	if (readtoken() == TPIPE) {
   		pipenode = (union node *)stalloc(sizeof (struct npipe));
 --- 245,259 ----

   STATIC union node *
   pipeline() {
 ! 	union node *n1, *n2, *pipenode;
   	struct nodelist *lp, *prev;
 + 	int negate;

 + 	negate = 0;
   	TRACE(("pipeline: entered\n"));
 + 	while (readtoken() == TNOT)
 + 		negate = !negate;
 + 	tokpushback++;
   	n1 = command();
   	if (readtoken() == TPIPE) {
   		pipenode = (union node *)stalloc(sizeof (struct npipe));
 ***************
 *** 267,273 ****
   		n1 = pipenode;
   	}
   	tokpushback++;
 ! 	return n1;
   }


 --- 272,284 ----
   		n1 = pipenode;
   	}
   	tokpushback++;
 ! 	if (negate) {
 !                 n2 = (union node *)stalloc(sizeof (struct nnot));
 !                 n2->type = NNOT;
 !                 n2->nnot.com = n1;
 !                 return n2;
 ! 	} else
 ! 		return n1;
   }


 diff -c3 -r sh-orig/trap.c sh/trap.c
 *** sh-orig/trap.c	Sun Mar 28 14:06:13 1999
 --- sh/trap.c	Sun Jan 23 12:39:59 2000
 ***************
 *** 322,328 ****
   		}
   		gotsig[i - 1] = 0;
   		savestatus=exitstatus;
 ! 		evalstring(trap[i]);
   		exitstatus=savestatus;
   	}
   done:
 --- 322,328 ----
   		}
   		gotsig[i - 1] = 0;
   		savestatus=exitstatus;
 ! 		evalstring(trap[i], 0);
   		exitstatus=savestatus;
   	}
   done:
 ***************
 *** 373,379 ****
   	handler = &loc1;
   	if ((p = trap[0]) != NULL && *p != '\0') {
   		trap[0] = NULL;
 ! 		evalstring(p);
   	}
   l1:   handler = &loc2;			/* probably unnecessary */
   	flushall();
 --- 373,379 ----
   	handler = &loc1;
   	if ((p = trap[0]) != NULL && *p != '\0') {
   		trap[0] = NULL;
 ! 		evalstring(p, 0);
   	}
   l1:   handler = &loc2;			/* probably unnecessary */
   	flushall();


From: Tanaka Akira <akr@jaist.ac.jp>
To: Martin Husemann <martin@rumolt.teuto.de>
Cc: gnats-bugs@gnats.netbsd.org
Subject: Re: bin/9194: /bin/sh -e may abort at a conditional in a loop
Date: 24 Jan 2000 00:07:03 +0900

 In article <200001231244.NAA03956@rumolt.teuto.de>,
   Martin Husemann <martin@rumolt.teuto.de> writes:

 > Here is a patch that fixes this problem and also PRs bin/9265 and bin/9266.

 and bin/9184.  Thanks.
 -- 
 Tanaka Akira
State-Changed-From-To: open->closed 
State-Changed-By: christos 
State-Changed-When: Thu Jan 27 18:40:31 EST 2000 
State-Changed-Why:  
Fixed, thanks 
>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.