From jilles@stack.nl  Tue Mar 31 23:03:19 2009
Return-Path: <jilles@stack.nl>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id D9B2510656C4
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 31 Mar 2009 23:03:19 +0000 (UTC)
	(envelope-from jilles@stack.nl)
Received: from mx1.stack.nl (unknown [IPv6:2001:610:1108:5000::149])
	by mx1.freebsd.org (Postfix) with ESMTP id 9A43C8FC1F
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 31 Mar 2009 23:03:19 +0000 (UTC)
	(envelope-from jilles@stack.nl)
Received: by mx1.stack.nl (Postfix, from userid 65534)
	id 594F53F6D7; Wed,  1 Apr 2009 01:03:18 +0200 (CEST)
Received: from snail.stack.nl (snail.stack.nl [IPv6:2001:610:1108:5010::131])
	by mx1.stack.nl (Postfix) with ESMTP id 8093A3F693
	for <FreeBSD-gnats-submit@freebsd.org>; Wed,  1 Apr 2009 01:03:16 +0200 (CEST)
Received: by snail.stack.nl (Postfix, from userid 1677)
	id D2EDF2289C; Wed,  1 Apr 2009 01:03:03 +0200 (CEST)
Message-Id: <20090331230303.D2EDF2289C@snail.stack.nl>
Date: Wed,  1 Apr 2009 01:03:03 +0200 (CEST)
From: Jilles Tjoelker <jilles@stack.nl>
Reply-To: Jilles Tjoelker <jilles@stack.nl>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: [PATCH] sh: empty line in eval resets $?
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         133255
>Category:       bin
>Synopsis:       [PATCH] sh(1): empty line in eval resets $?
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    stefanf
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Mar 31 23:10:01 UTC 2009
>Closed-Date:    Sun May 16 10:22:13 UTC 2010
>Last-Modified:  Sun May 16 10:22:13 UTC 2010
>Originator:     Jilles Tjoelker
>Release:        FreeBSD 7.2-PRERELEASE i386
>Organization:
MCGV Stack
>Environment:
bug seems to be present in -CURRENT also
>Description:
An empty line in the text passed to 'eval' and 'trap' and to the '-c' option
causes sh to forget the exit status of the previous command. 'fc' may be
affected as well. This only happens if the the empty line is at the top
level of the string (not in {}, if and the like).

git sometimes does this, with eval. This may cause a test to succeed when
it has really failed.

Other shells preserve the exit status in this case.
>How-To-Repeat:
Input: (to /bin/sh) (the empty line between 'false and '; is significant)

eval 'false

'; echo $?

Expected result:
1

Actual result:
0

Two other inputs with the same property:

eval 'false

echo $?'

sh -c 'false

echo $?'

An input not affected by the bug:

eval '{
false

echo $?
}'

properly prints 1.

>Fix:

The following patch is inspired by how empty lines are ignored in
shell scripts and in interactive mode (in either case, the exit status is
preserved).

I have run a 7.x buildworld/installworld and various configure scripts
with this, and have not noticed any problems.

--- sh-eval-emptyline.patch begins here ---
--- src/bin/sh/eval.c.orig	2008-09-04 19:34:53.000000000 +0200
+++ src/bin/sh/eval.c	2009-03-22 22:20:17.000000000 +0100
@@ -166,7 +166,8 @@
 	setstackmark(&smark);
 	setinputstring(s, 1);
 	while ((n = parsecmd(0)) != NEOF) {
-		evaltree(n, 0);
+		if (n != NULL)
+			evaltree(n, 0);
 		popstackmark(&smark);
 	}
 	popfile();
--- sh-eval-emptyline.patch ends here ---


>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->patched  
State-Changed-By: brucec 
State-Changed-When: Thu Apr 9 23:10:43 UTC 2009 
State-Changed-Why:  
Fixed in r190698 

http://www.freebsd.org/cgi/query-pr.cgi?pr=133255 
Responsible-Changed-From-To: freebsd-bugs->stefanf 
Responsible-Changed-By: stefanf 
Responsible-Changed-When: Mon Apr 13 20:21:43 UTC 2009 
Responsible-Changed-Why:  
Take. 

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

From: Eygene Ryabinkin <rea-fbsd@codelabs.ru>
To: bug-followup@freebsd.org
Cc: stefanf@freebsd.org
Subject: Re: bin/133255: [PATCH] sh(1): empty line in eval resets $?
Date: Thu, 30 Apr 2009 15:40:53 +0400

 Gentlemen, good day.
 
 I can confirm that the patch fixes the issue -- I had run into it too
 (with Git testsuite) and had verified the patch on 7.2-PRERELEASE.
 
 I would propose to add regression test for this bug:
 --- regression-suite-add-test-for-bin-133255.diff begins here ---
 =46rom ffe433ab90edec159d3dc8b6237eb4503ff44895 Mon Sep 17 00:00:00 2001
 =46rom: Eygene Ryabinkin <rea-fbsd@codelabs.ru>
 Date: Wed, 29 Apr 2009 17:33:34 +0400
 Subject: [PATCH] regression suite: add test for bin/133255 (/bin/sh eval is=
 sue)
 
 PR in question is
   http://www.freebsd.org/cgi/query-pr.cgi?pr=3Dbin/133255
 
 Signed-off-by: Eygene Ryabinkin <rea-fbsd@codelabs.ru>
 ---
  tools/regression/bin/sh/builtins/eval.0 |    7 +++++++
  1 files changed, 7 insertions(+), 0 deletions(-)
  create mode 100644 tools/regression/bin/sh/builtins/eval.0
 
 diff --git a/tools/regression/bin/sh/builtins/eval.0 b/tools/regression/bin=
 /sh/builtins/eval.0
 new file mode 100644
 index 0000000..731edae
 --- /dev/null
 +++ b/tools/regression/bin/sh/builtins/eval.0
 @@ -0,0 +1,7 @@
 +#$FreeBSD$
 +
 +eval '
 +false
 +
 +' && exit 1
 +exit 0
 --=20
 1.6.2.4
 --- regression-suite-add-test-for-bin-133255.diff ends here ---
 --=20
 Eygene
  _                ___       _.--.   #
  \`.|\..----...-'`   `-._.-'_.-'`   #  Remember that it is hard
  /  ' `         ,       __.--'      #  to read the on-line manual
  )/' _/     \   `-_,   /            #  while single-stepping the kernel.
  `-'" `"\_  ,_.-;_.-\_ ',  fsc/as   #
      _.-'_./   {_.'   ; /           #    -- FreeBSD Developers handbook
     {_.-``-'         {_/            #
State-Changed-From-To: patched->closed 
State-Changed-By: stefanf 
State-Changed-When: Sun May 16 10:21:39 UTC 2010 
State-Changed-Why:  
Merged back to 7. The proposed regression test is also already in the tree. 

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