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

Re: memory leak (2): named reference



> 2024/08/06 7:53, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> 
> On Fri, Jun 28, 2024 at 3:19 AM Jun T <takimoto-j@xxxxxxxxxxxxxxxxx> wrote:
> 
>> % cat test3
>> typeset -n ref
>> for ref in one ref
>> do; done
> 
>> test3 also causes two memory leaks.
>> One is the same as test2

>> In the other, 4 bytes ("ref", allocated by ztrdup() at params.c:6329)
(yes, what was leaking was 4 bytes for "one")
>> are lost. This is caused by aborting the loop by the self reference

Sorry I was wrong. The leak was not due to the "self-reference" error.
The following gives no error but still causes the leak:

% cat test4
typeset -n ref
for ref in a b2 c2345
do; done

valgrind says 5 bytes ("a" and "b2") are lost. It seems the problem is in
the macro SETREFNAME() used in setloopvar(). It overwrites pm->u.str
without freeing the old value. How about the following?


diff --git a/Src/params.c b/Src/params.c
index f143a790f..b710ddbf6 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -482,7 +482,8 @@ static initparam argvparam_pm = IPDEF9("", &pparams, NULL, \
 #define GETREFNAME(PM) (((PM)->node.flags & PM_SPECIAL) ?	\
 			(PM)->gsu.s->getfn(PM) : (PM)->u.str)
 #define SETREFNAME(PM,S) (((PM)->node.flags & PM_SPECIAL) ?		\
-			  (PM)->gsu.s->setfn(PM,(S)) : ((PM)->u.str = (S)))
+			  (PM)->gsu.s->setfn(PM,(S)) : \
+			  (zsfree((PM)->u.str), (PM)->u.str = (S)))
 
 static Param argvparam;
 






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