Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Memory leak when working with undefined associative array keys & problems with unset
- X-seq: zsh-workers 41719
- From: Anssi Palin <Anssi.Palin@xxxxxxxxxxxx>
- To: "zsh-workers@xxxxxxx" <zsh-workers@xxxxxxx>
- Subject: Memory leak when working with undefined associative array keys & problems with unset
- Date: Sat, 16 Sep 2017 20:57:06 +0000
- Accept-language: fi-FI, en-US
- Authentication-results: spf=none (sender IP is ) smtp.mailfrom=Anssi.Palin@xxxxxxxxxxxx;
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudomnia.onmicrosoft.com; s=selector1-edu-omnia-fi; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=Nj9YqojTFg6ntCt4WcjefkULbT/xV8WYncKgmQXqFow=; b=IFT8XbgOhXGQqiFzCCDcfKZvEGKEShCEoUGfeEKIFnRaqcd1pSdMzHxTNHgCx/7gEibO3vm8ermS5qzJtXWuy7Bc+qUTkPA//BvjfjjJxY74uQKB8cyi2kj6pa0jlmHZgCQU5U+r7+GXwxMC/yA2/i60wGlPTFUw3Z9xvYWZDuE=
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
- Spamdiagnosticmetadata: NSPM
- Spamdiagnosticoutput: 1:99
- Thread-index: AQHTLy3wuTzbYeraakiQ/7v849gZ8Q==
- Thread-topic: Memory leak when working with undefined associative array keys & problems with unset
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.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author