Re: bash memory leak when querying associative array for nonexisting element

2022-11-21 Thread Chet Ramey

On 11/20/22 8:02 AM, Ralf Oehler wrote:


Bash Version: 5.2
Patch Level: 2
Release Status: release

Description:
I want to read an associative array. Like so: y="${aa[$i]}"
If aa contains the element queried, then everything works as expected
If aa does not contain such an element, the result is the empty string, 
which is expected, but the query permanently increases the memory consumption 
of the executing bash, which is not expected. The program below demonstrates 
this behaviour. When run, the bash process increases its memory indefinitely.

I did not find a fix.


Thanks for the report. Koichi's fix is a good one.

Chet

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/




Re: bash memory leak when querying associative array for nonexisting element

2022-11-20 Thread Koichi Murase
 2022年11月20日(日) 23:07 Ralf Oehler :
> Bash Version: 5.2
> Patch Level: 2
> Release Status: release
>
> Description:
> [Detailed description of the problem, suggestion, or
> complaint.]
> I want to read an associative array. Like so: y="${aa[$i]}"
> If aa contains the element queried, then everything works as
> expected
> If aa does not contain such an element, the result is the
> empty string, which is expected, but the query permanently
> increases the memory consumption of the executing bash,
> which is not expected. The program below demonstrates this
> behaviour. When run, the bash process increases its memory
> indefinitely.

I also noticed a memory leak early this month in testing my new
extglob implementation and was planning to submit a patch later. I now
checked this report and confirmed that this is actually caused by the
same part of the code. Here, I attach my patch
[r0037.parameter_brace_expand_word.memleak.patch], though I haven't
yet carefully tested it because I originally planned to submit it
sometime later.

--
Koichi
From 4183e204ea0f954de42dea6ba2ec0f47a061f7f3 Mon Sep 17 00:00:00 2001
From: Koichi Murase 
Date: Thu, 3 Nov 2022 14:01:41 +0900
Subject: [PATCH 1/2] fix memleak in parameter_brace_expand_word

---
 subst.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/subst.c b/subst.c
index 7ec8f8e9..a018b1b1 100644
--- a/subst.c
+++ b/subst.c
@@ -7507,8 +7507,6 @@ expand_arrayref:
? quote_string (temp)
: quote_escapes (temp);
  rflags |= W_ARRAYIND;
- if (estatep)
-   *estatep = es;  /* structure copy */
}
   /* Note that array[*] and array[@] expanded to a quoted null string by
 returning the W_HASQUOTEDNULL flag to the caller in addition to TEMP. 
*/
@@ -7517,7 +7515,9 @@ expand_arrayref:
   else if (es.subtype == 2 && temp && QUOTED_NULL (temp) && (quoted & 
(Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
rflags |= W_HASQUOTEDNULL;
 
-  if (estatep == 0)
+  if (etastep)
+   *estatep = es;  /* structure copy */
+  else
flush_eltstate ();
 }
 #endif
-- 
2.37.2