From nobody@FreeBSD.org  Tue Feb 26 18:27:04 2013
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1])
	by hub.freebsd.org (Postfix) with ESMTP id E70564A9
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 26 Feb 2013 18:27:04 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from red.freebsd.org (red.freebsd.org [IPv6:2001:4f8:fff6::22])
	by mx1.freebsd.org (Postfix) with ESMTP id D8274162D
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 26 Feb 2013 18:27:04 +0000 (UTC)
Received: from red.freebsd.org (localhost [127.0.0.1])
	by red.freebsd.org (8.14.5/8.14.5) with ESMTP id r1QIR498017393
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 26 Feb 2013 18:27:04 GMT
	(envelope-from nobody@red.freebsd.org)
Received: (from nobody@localhost)
	by red.freebsd.org (8.14.5/8.14.5/Submit) id r1QIR4MT017392;
	Tue, 26 Feb 2013 18:27:04 GMT
	(envelope-from nobody)
Message-Id: <201302261827.r1QIR4MT017392@red.freebsd.org>
Date: Tue, 26 Feb 2013 18:27:04 GMT
From: "ZAHEMSZKY, Gabor" <Gabor@Zahemszky.HU>
To: freebsd-gnats-submit@FreeBSD.org
Subject: strange behaviour of the $(( )) form in sh(1) 
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         176444
>Category:       bin
>Synopsis:       strange behaviour of the $(( )) form in sh(1)
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    jilles
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Feb 26 18:30:00 UTC 2013
>Closed-Date:    Sat Aug 24 21:52:12 UTC 2013
>Last-Modified:  Sat Aug 24 21:52:12 UTC 2013
>Originator:     ZAHEMSZKY, Gabor
>Release:        9.1-RELEASE
>Organization:
Zahemszky Ltd
>Environment:
FreeBSD Vasarely.Zahemszky.HU 9.1-RELEASE FreeBSD 9.1-RELEASE #0 r243826: Tue Dec  4 06:55:39 UTC 2012     root@obrian.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC  i386

>Description:
In "man sh" it's missing the ++ or -- operator from the description of Arithmetic Expansion. It's OK, as FreeBSD's sh doesn't understand it. But! The main problem, that sh isn't very intelligent in the handling of that form. Look:

$ sh
$ a=1
$ : $(( a++ ))
arithmetic expression: expecting primary: " a++ "
$ echo $a
1
$ : $(( ++a ))
$ echo $a
1
$ : $(( a-- ))
arithmetic expression: expecting primary: " a-- "
$ : $(( --a ))
$ echo $a
1

As you can see, sh correctly generate an error message, if I try to use the postincrement/postdecrement form, but silently ignore my command, when I use the preincrement/predecrement form.
Acrually, at least pdksh and bash knows it, so it should be better if sh understand these operators, too. Or, if it couldn't, it should generate errors in the "pre"-form.
>How-To-Repeat:
Try typing my example commands
>Fix:


>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->jilles 
Responsible-Changed-By: jilles 
Responsible-Changed-When: Sat Mar 2 23:55:19 UTC 2013 
Responsible-Changed-Why:  
sh(1) is my area. 

It is indeed unfortunate that $((++a)) is so insidiously broken. 
I don't know yet whether to fix it by implementing the operators 
or by adding error detection. 

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

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/176444: commit references a PR
Date: Sat, 24 Aug 2013 20:06:12 +0000 (UTC)

 Author: jilles
 Date: Sat Aug 24 20:06:00 2013
 New Revision: 254806
 URL: http://svnweb.freebsd.org/changeset/base/254806
 
 Log:
   sh: Reject ++ and -- in arithmetic.
   
   POSIX does not require ++ and -- in arithmetic. It is probably more useful
   to reject them than to treat ++x and --x as x silently.
   
   Note that the behaviour of increment and decrement can be obtained via
   (x+=1), ((x+=1)-1), (x-=1) and ((x-=1)+1).
   
   PR:		bin/176444
 
 Added:
   head/tools/regression/bin/sh/expansion/arith13.0   (contents, props changed)
 Modified:
   head/bin/sh/arith_yylex.c
 
 Modified: head/bin/sh/arith_yylex.c
 ==============================================================================
 --- head/bin/sh/arith_yylex.c	Sat Aug 24 19:58:36 2013	(r254805)
 +++ head/bin/sh/arith_yylex.c	Sat Aug 24 20:06:00 2013	(r254806)
 @@ -218,9 +218,13 @@ checkeqcur:
  			value += ARITH_REM - '%';
  			goto checkeq;
  		case '+':
 +			if (buf[1] == '+')
 +				return ARITH_BAD;
  			value += ARITH_ADD - '+';
  			goto checkeq;
  		case '-':
 +			if (buf[1] == '-')
 +				return ARITH_BAD;
  			value += ARITH_SUB - '-';
  			goto checkeq;
  		case '~':
 
 Added: head/tools/regression/bin/sh/expansion/arith13.0
 ==============================================================================
 --- /dev/null	00:00:00 1970	(empty, because file is newly added)
 +++ head/tools/regression/bin/sh/expansion/arith13.0	Sat Aug 24 20:06:00 2013	(r254806)
 @@ -0,0 +1,6 @@
 +# $FreeBSD$
 +# Pre-increment and pre-decrement in arithmetic expansion are not in POSIX.
 +# Require either an error or a correct implementation.
 +
 +! (eval 'x=4; [ $((++x)) != 5 ] || [ $x != 5 ]') 2>/dev/null &&
 +! (eval 'x=2; [ $((--x)) != 1 ] || [ $x != 1 ]') 2>/dev/null
 _______________________________________________
 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->closed 
State-Changed-By: jilles 
State-Changed-When: Sat Aug 24 21:51:14 UTC 2013 
State-Changed-Why:  
Error detection has been added to head; no MFC is planned. 

Thanks for the report. 

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