Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Tcl_SetVar2() leaks memory

Closed / Fixed7368d225a657380c364ceb3fcf814bdba374a566Created 12 years agoUpdated 12 years ago
Descriptionplain2014-07-10 14:40:59
Tcl_SetVar2() creates the (Tcl_Obj) value out of
the (const char *) data it receives, then passes
it on to Tcl_SetVar2Ex() to manage the rest of its
lifetime.  

This mostly works, so long as Tcl_SetVar2Ex() stores
the value in a variable.

When flags includes TCL_APPEND_VALUE and does not include
TCL_LIST_ELEMENT that does not happen and the mem leaks.
There may be other leak scenarios too.

The testing package pkgua exposes this, when examined with
valgrind.
Referenced by2
  • d9d00aa893[7368d225a6] Extend the auto-cleanup of zero ref count values passed in to the Tcl_*SetVar*() family of routines to cover the missing case where the flags value of TCL_APPEND_VALUE is passed in alone. *** POTENTIAL INCOMAPTIBILITY*** check-in2014-07-10 16:40:30
  • 212c0638b1[7368d225a6] Extend the auto-cleanup of zero ref count values passed in to the Tcl_*SetVar*() family of routines to cover the missing case where the flags value of TCL_APPEND_VALUE is passed in alone. *** POTENTIAL INCOMAPTIBILITY*** check-in2014-07-10 15:45:32
dgpplain2014-07-10 14:59:24
Another likely leak scenario is
a variable name that cannot be created.
dgpplain2014-07-10 15:04:19
Um, no.  The latter case is handled.
dgpplain2014-07-10 15:55:34
This is a gap in the fix for bug 1334947.

http://core.tcl.tk/tcl/info/1334947

For all the same reasons that was fixed in 
a mildly incompatible way, this should also
be treated the same, with the Tcl_*SetVar*
machinery taking care of all zero-refcount
value housekeeping to relieve all callers
of that burden.

Those harmed will be anyone with code like this:

Tcl_Obj *value = Tcl_New...();
Tcl_SetVar2Ex(interp, "foo", NULL, value, TCL_APPEND_VALUE);
Tcl_DecrRefCount(value);

Anyone with such code likely has the Tcl_DecrRefCount()
call in place only because they added it to plug the memleak
that would otherwise be observed.  With this change to
the core, that code will have to modified to continue working
and avoid a segfault (same as we did with the other 1334947
changes).  If the code must continue running with older Tcl
releases, then the fix is to add a

Tcl_IncrRefCount(value);

call prior to SetVar so that refcount management is balanced.

Any such code with the luxury of ditching all old Tcl releases and
claiming support of solely 8.5.16+ and 8.6.2+ can just drop
the Tcl_DecrRefCount() call altogether and take advantage of
the improved interface standard.

I expect to count the number of impacted users on one hand.
TCL_APPEND_VALUE is just not a popular flag option here in
my experience.
dgpplain2014-07-10 16:41:32
Fixed for 8.5.16 and 8.6.2
Log in (anonymous is fine) to add a comment.