[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Some issues with short-circuiting arithmetic operators
From: |
Chet Ramey |
Subject: |
Re: Some issues with short-circuiting arithmetic operators |
Date: |
Sun, 09 Sep 2012 16:41:18 -0400 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:14.0) Gecko/20120713 Thunderbird/14.0 |
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 9/5/12 10:40 PM, Dan Douglas wrote:
> This reorder function is meant to swap values of a two-element array if
> unordered. Bash and ksh produce reversed results. mksh and zsh do as expected.
[...]
> The Ksh issue seems to be that an explicit x[0] is needed (it's a slightly
> outdated dev build), but I can't figure out why Bash is doing this. No
> parameter expansion in the arithmetic does the same:
>
> function reorder2 {
> _=$x let '(x[1] < x) && (x=x[1], x[1]=_)'
> echo "${x[@]}"
> }
>
> Some variations crash:
>
> $ bash -c 'function reorder { (( x[1] < x[0] && (x=x[1], x[1]=$x) ));
> echo "${x[@]}"; }; x=(123 456); reorder; x=(456 123); reorder'
> Segmentation fault
> $ bash -c 'function reorder { (( (x > x[1]) && (x=${x[1]}, x[1]=$x) ));
> echo "${x[@]}"; }; x=(123 456); reorder; x=(456 123); reorder'
> 123 456
> Segmentation fault
>
> The second issue is that Bash tries to resolve arithmetic variables when
> evaluation should never reach them.
There is a pointer aliasing error that causes the seg fault, but all of
these errors were caused by incorrect handling of the `noeval' state.
These will all be fixed in the next version of bash.
Chet
- --
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU chet@case.edu http://cnswww.cns.cwru.edu/~chet/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAlBM/uMACgkQu1hp8GTqdKtBZQCbB/9iJAMUvp4KFxisPaDWIlSU
HakAnRUJJUzr1WTlWKeeSHCJO49R5MDO
=+u40
-----END PGP SIGNATURE-----