Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Memory leak when working with undefined associative array keys & problems with unset


I've run into two problems with the way Zsh handles associative array keys, both tested on Linux with Zsh 5.4.1 compiled from source and 5.2 from the Ubuntu repositories.

Zsh appears to permanently allocate some memory when just checking if a key is defined in an associative array. This behavior resulted in system memory getting exhausted on my machine with a script that checks random generated strings against a word lookup table. The following snippet replicates the bug and results in some 100 megabytes of memory being allocated:

typeset -A a
for (( i = 0; i < 1000000; i++ )); do
    (( ${+a[$i]} ))

Iterating over the same set of undefined keys a second time does not seem to cause more memory to be allocated. Moreover, unsetting or emptying the array doesn't appear to free all of the memory even though the array is destroyed or, in the latter case, emptied as expected. On 5.4.1 unsetting or emptying a second time looks to finally be freeing all of the taken up memory. Unsetting individual keys afterwards in a loop similar to the example above has the same problem, but unsetting each key immediately after checking it seems to mitigate this.

The second issue I have pertains to special characters in associative array keys when unsetting them individually:

$ key='hello * [ world'
$ typeset -A a=("$key" val)
$ unset "a[$key]"
unset: a[hello * [ world]: invalid parameter name

Since characters such as '\', '[', ']', '(' and ')' must be escaped but others like space or '*' shouldn't be, using the q or b parameter expansion flags is out of the question:

$ unset "a[${(q)key}]"
unset: a[hello\ \*\ \[\ world]: invalid parameter name
$ typeset -p a
typeset -A a=( 'hello * [ world' val )

The latter unset error message only shows up on 5.2 but in both cases the key remains set. Similar problems with special characters seem to affect [['s -v flag when checking for existence of associative array keys.

Thank you.

Messages sorted by: Reverse Date, Date, Thread, Author