Skip to content

fix: memory leak in InsideDollar #652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions check/fixes.frm
Original file line number Diff line number Diff line change
Expand Up @@ -3789,3 +3789,25 @@ P G;
assert succeeded?
assert result("G") =~ expr("389")
*--#] PullReq535 :
*--#[ PullReq652 :
#-
Off statistics;
#$a = 0;
Local test = 1;
$b = 0;
* Test no leaks when working inside a 0 dollar variable
#inside $a
Multiply 2;
#endinside
Inside $a;
Multiply 2;
EndInside;
Inside $b;
Multiply 2;
EndInside;
ModuleOption local $a,$b;
Print;
.end
assert succeeded?
assert result("test") =~ expr("1")
*--#] PullReq652 :
19 changes: 14 additions & 5 deletions sources/dollar.c
Original file line number Diff line number Diff line change
Expand Up @@ -1756,9 +1756,9 @@ int InsideDollar(PHEAD WORD *ll, WORD level)
olddefer = AR.DeferFlag;
AR.DeferFlag = 0;
while ( --numvar >= 0 ) {
numdol = *ll++;
d = Dollars + numdol;
{
numdol = *ll++;
d = Dollars + numdol;
{
#ifdef WITHPTHREADS
int nummodopt, dtype = -1;
if ( AS.MultiThreaded && ( AC.mparallelflag == PARALLELFLAG ) ) {
Expand All @@ -1778,7 +1778,16 @@ int InsideDollar(PHEAD WORD *ll, WORD level)
}
#endif
newd = DolToTerms(BHEAD numdol);
if ( newd == 0 || newd->where[0] == 0 ) continue;
if ( newd == 0 ) {
continue;
}
if ( newd->where[0] == 0 ) {
// DolToTerms potentially allocates memory. Free it.
// The free below is inside the while loop.
if ( newd->factors ) M_free(newd->factors,"Dollar factors");
M_free(newd,"Copy of dollar variable");
continue;
}
r = newd->where;
NewSort(BHEAD0);
while ( *r ) { /* Sum over the terms */
Expand Down Expand Up @@ -1822,7 +1831,7 @@ int InsideDollar(PHEAD WORD *ll, WORD level)
#endif
if ( newd->factors ) M_free(newd->factors,"Dollar factors");
M_free(newd,"Copy of dollar variable");
}
}
}
idcall:;
AR.Cnumlhs = oldnumlhs;
Expand Down
Loading