Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 786e6925df4cac2aef9b3e2a35c7e62865227ea3
Ticket: 4dbdd9af1439d874381fd6fdb968bcb0d596f0bb
TclDeleteNamespaceVars leaks when unset trace revives var under deletion
User & Date: mr_calvin on 2016-08-31 12:20:31
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/plain"
  4. comment changed to:
    When an unset trace upon namespace deletion (TclDeleteNamespaceVars) brings back a namespace variable (ie. defines it by assigning a value), the corresponding Var(InHash) is not freed:
    
    ==32261== 64 bytes in 1 blocks are definitely lost in loss record 3 of 3
    ==32261==    at 0x4C2BBAD: malloc (vg_replace_malloc.c:299)
    ==32261==    by 0x4E75392: TclpAlloc (tclAlloc.c:699)
    ==32261==    by 0x4E8CE23: Tcl_Alloc (tclCkalloc.c:1059)
    ==32261==    by 0x4FE532C: AllocVarEntry (tclVar.c:6013)
    ==32261==    by 0x4F704D7: CreateHashEntry (tclHash.c:359)
    ==32261==    by 0x4FDC8AD: VarHashCreateVar (tclVar.c:63)
    ==32261==    by 0x4FDD7A4: TclLookupSimpleVar (tclVar.c:853)
    ==32261==    by 0x4FDD130: TclObjLookupVarEx (tclVar.c:615)
    ==32261==    by 0x4FE2E09: Tcl_VariableObjCmd (tclVar.c:4617)
    ==32261==    by 0x4E80709: Dispatch (tclBasic.c:4358)
    ==32261==    by 0x4E8078F: TclNRRunCallbacks (tclBasic.c:4391)
    ==32261==    by 0x4E80065: Tcl_EvalObjv (tclBasic.c:4121)
    
    To reproduce:
    
    1) apply patch
    2) run:
    
    valgrind --leak-check=full --track-origins=yes ./tcltest /usr/local/src/tcl/tests/all.tcl -singleproc 1 -file var.test
    
    (see the test case var-8.3 added to var.test by the patch attached)
    
    The proposed fix is to call TclSetVarUndefined(varPtr) after having triggered any traces, as hinted at by the existing, but unimplemented comment:
    
    "[...] force it *undefined* in case an unset trace brought it back from the dead."
    
    Without that, the indirectly called FreeVarEntry() will not free the revived Var in question.
    As far as I can judge, this affects *all* Tcl versions 8.5.*, 8.6.*, and 8.7.*, as the code in question appears unaltered to me between major versions.
    
    The unset trace (in this test) also gives rise to a LiteralEntry leak (due BC'ing the unset proc):
    
    ==32261==    at 0x4C2BBAD: malloc (vg_replace_malloc.c:299)
    ==32261==    by 0x4E75392: TclpAlloc (tclAlloc.c:699)
    ==32261==    by 0x4E8CE23: Tcl_Alloc (tclCkalloc.c:1059)
    ==32261==    by 0x4F9DD86: TclCreateLiteral (tclLiteral.c:236)
    ==32261==    by 0x4F9E0F4: TclRegisterLiteral (tclLiteral.c:434)
    ==32261==    by 0x4EE96FF: TclCompileSetCmd (tclCompCmdsSZ.c:218)
    ==32261==    by 0x4F4B4E8: TclAttemptCompileProc (tclEnsemble.c:3212)
    ==32261==    by 0x4F2A21A: CompileCmdCompileProc (tclCompile.c:1950)
    ==32261==    by 0x4F2A820: CompileCommandTokens (tclCompile.c:2065)
    ==32261==    by 0x4F2ACF4: TclCompileScript (tclCompile.c:2196)
    ==32261==    by 0x4F2BF9A: TclCompileTokens (tclCompile.c:2445)
    ==32261==    by 0x4EB093D: TclCompileConcatCmd (tclCompCmds.c:816)
    
    Don't know yet how to fix that one (as the literal machinery such as TclReleaseLiteral is not available in this context, and should not be, most probably).
    
  5. foundin changed to: "8.7.0a"
  6. is_private changed to: "0"
  7. login: "mr_calvin"
  8. priority changed to: "5 Medium"
  9. private_contact changed to: "d4d1d8192a46acf2c7d58cddc1040ecdbc9c9d77"
  10. resolution changed to: "None"
  11. severity changed to: "Severe"
  12. status changed to: "Open"
  13. submitter changed to: "mr_calvin"
  14. subsystem changed to: "07. Variables"
  15. title changed to:
    TclDeleteNamespaceVars leaks when unset trace revives var under deletion
    
  16. type changed to: "Bug"