help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Bash-5.2 Release available


From: Corey Hickey
Subject: Re: Bash-5.2 Release available
Date: Fri, 28 Oct 2022 18:22:22 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.3.1

On 2022-09-26 12:33, Chet Ramey wrote:
There are a few incompatible changes between bash-5.1 and bash-5.2. Here-
documents and here-strings use temporary files if the shell compatibility
level is 50 or lower. The `unset' builtin in bash-5.2 treats array subscripts
`@' and `*' differently than previous versions, and differently depending on
whether the array is indexed or associative. Bash-5.2 attempts to prevent
double-expansion of array subscripts under certain circumstances, especially
arithmetic evaluation, by acting as if the `assoc_expand_once' shell option
were set. Set the compatibility level appropriately to revert to previous
behavior; details are in the file COMPAT.

I think I encountered the array subscript change, and I'm having some trouble.
I'm working with arithmetic evaluation of an associative array that is
subscripted by a variable with potentially arbitrary data. The security
context isn't really that critical (I don't expect malicious content), but
still, I want to do the right thing and have resilient code. I've read that
this is problematic due to POSIX requirements.

https://unix.stackexchange.com/questions/627474/how-to-use-associative-arrays-safely-inside-arithmetic-expressions

The following is reproduced on git a99d905216cc0aac5de0c3050f4afc54e21c6bc5, compiled via 
"./configure && make -j8".

$ ./bash --version
GNU bash, version 5.2.2(1)-release (x86_64-pc-linux-gnu)

The method that I thought was the best, based on that stackexchange page
(and I could be wrong) was to escape the subscript variable. This at
least seemed to work correctly on bash 5.1, and I can reproduce it on
bash 5.2 via a compat level.

$ ./bash -c 'BASH_COMPAT=51 ; declare -A assoc ; var=@ ; assoc[$var]=1 ; echo "$((1 
+ assoc[\$var]))"'
2

I'm using '@' for the variable as a deliberately pathological value.
With bash 5.2 defaults, I can't find a way to make this work. Keeping
the subscript variable escaped results in an evaluation to an undefined
key (apparently):

$ ./bash -c 'declare -A assoc ; var=@ ; assoc[$var]=1 ; echo "$((1 + 
assoc[\$var]))"'
1

That makes sense, given the description of the change. If I un-escape
the subscript variable, though, I get an error:

$ ./bash -c 'declare -A assoc ; var=@ ; assoc[$var]=1 ; echo "$((1 + 
assoc[$var]))"'
./bash: line 1: assoc[@]: bad array subscript
1

Is there a reasonable way to do this? I could work around it with
a temporary variable:

$ ./bash -c 'declare -A assoc ; var=@ ; assoc[$var]=1 ; tmp=${assoc[$var]} ; echo 
"$((1 + tmp))"'
2

...but I'm still interested in knowing if there is a more correct
solution available.

Thank you,
Corey



reply via email to

[Prev in Thread] Current Thread [Next in Thread]