zsh-workers
 help / color / mirror / code / Atom feed
* Memory leak when working with undefined associative array keys & problems with unset
@ 2017-09-16 20:57 Anssi Palin
  2017-09-17 23:15 ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Anssi Palin @ 2017-09-16 20:57 UTC (permalink / raw)
  To: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 1986 bytes --]

Hello,

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]} ))
done

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.


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2017-09-24 11:48 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-16 20:57 Memory leak when working with undefined associative array keys & problems with unset Anssi Palin
2017-09-17 23:15 ` Bart Schaefer
     [not found]   ` <1505690609.1747008.1109155032.651AC839@webmail.messagingengine.com>
2017-09-18  0:22     ` Bart Schaefer
2017-09-23 17:59   ` Peter Stephenson
2017-09-24  1:48     ` Bart Schaefer
2017-09-24 11:37       ` Daniel Shahaf

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).