On Mon, Jan 11, 2021 at 09:07:08AM -0500, Chet Ramey wrote:
> On 1/10/21 2:55 PM, Oğuz wrote:
> 
> > If I don't know the key beforehand, like if I read it from a file or the
> > like, this becomes a security issue. That is my concern,
> > `assoc_expand_once' doesn't help in that situation.
> 
> If you're taking input that you haven't verified and putting it into an
> expansion context, I recommend you reconsider.
> 
> You can also use the key-value pair assignment syntax, which doesn't
> require scanning for a close brace.

So what I'm hearing is:

It is not safe to use an associative array element inside a math
context.  This is either by design, or an unavoidable side effect of the
implementation, and it will not be fixed, and there is no sense arguing
about it.

unicorn:~$ echo "$BASH_VERSION"
5.1.0(1)-release
unicorn:~$ declare -A aa
unicorn:~$ x='y[$(date >&2)0]'
unicorn:~$ aa[$x]=1
unicorn:~$ echo "${aa[$x]}"
1
unicorn:~$ echo "$(( aa[$x] ))"
Mon Jan 11 09:25:48 EST 2021
0

The workaround is to retrieve the AA element first, then perform the
arithmetic you need to perform using only string variables or indexed
array elements, then store the result back into the AA element if desired.

unicorn:~$ (( aa[$x]++ ))
Mon Jan 11 09:29:24 EST 2021
Mon Jan 11 09:29:24 EST 2021
unicorn:~$ tmp=${aa[$x]}; ((tmp++)); aa[$x]=$tmp
unicorn:~$ declare -p aa
declare -A aa=(["y[\$(date >&2)0]"]="2" ["y[0]"]="1" )

Reply via email to