Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 61425017d528d0fd8c25fef098321d9ff05128b8b75f22c165cea735a87c7875
Ticket: 578155d5a19b348dc1a9fe96cc2c067a59326a89
Very rare bug (segfault) if set variable (with error case) using self-releasable object as new value
User & Date: dgp on 2018-01-11 20:24:10
Changes

  1. icomment:
    >   1) The function checks the refCount only AFTER trying of set the variable
    > (which can change the refCount of the object by calling of the traces, etc).
    
    That's false.  Look at the code.
    
    https://core.tcl.tk/tcl/artifact/1f16d4aba6?ln=1718-1728
    
      1718      varPtr = TclObjLookupVarEx(interp, part1Ptr, part2Ptr, flags, "set",
      1719  	    /*createPart1*/ 1, /*createPart2*/ 1, &arrayPtr);
      1720      if (varPtr == NULL) {
      1721  	if (newValuePtr->refCount == 0) {
      1722  	    Tcl_DecrRefCount(newValuePtr);
      1723  	}
      1724  	return NULL;
      1725      }
      1726  
      1727      return TclPtrSetVarIdx(interp, varPtr, arrayPtr, part1Ptr, part2Ptr,
      1728  	    newValuePtr, flags, -1);
    
    The Decrement comes only after a failed lookup attempt, which calls no traces.
    Traces are called from within TclPtrSetVarIdx(). The lookup routine also
    doesn't take newValuePtr as an argument. The only "callback" opportunities
    I see in the lookup routines is the invocation of variable resolvers.
    
    > 2) For the developer using Tcl_ObjSetVar, it is very bad up to impossible
    > to act plausible in error case, because he don't know when he should decrease
    > refCount in error case or not, 
    
    Here's the framework for a caller to cover all the cases.
    If the caller wants to prevent any free of "value" by Tcl_ObjSetVar2(),
    it can call Tcl_IncrRefCount(value) before calling Tcl_ObjSetVar2()
    and call Tcl_DecrRefCount(value) after it no longer will use "value".
    This will manage the lifetime without leaks.  If the caller has no need
    to use "value" other than passing it to Tcl_ObjSetVar2(), then the code
    below does it.
    
    Tcl_Obj *value; /* Either create or receive as argument, any refcount. */
    Tcl_Obj *stored;
    Tcl_Obj *varName = Tcl_NewStringObj("foo", -1);
    Tcl_IncrRefCount(varName);
    stored = Tcl_ObjSetVar2(interp, varName, NULL, value, TCL_LEAVE_ERR_MSG);
    Tcl_DecrRefCount(varName);
    if (stored == NULL) {
        /* If value had refcount 0, it has been freed. */
        /* Otherwise, it still has the refcount it was called with. */
    } else if (stored == value) {
        /* The variable "foo" holds value */
        /* The refcount of value one greater than it was called with. */
    } else {
        /* The variable "foo" holds stored, which is different from value. */
        /* Traces intervened. */
        /* If value had refcount 0, it is freed. */
        /* Otherwise, it still has the refcount it was called with. */
    }
    
    This approach is very convenient for callers. In most cases they
    need not deal with refcounts of "value" at all.
    
    If there's some scenario that framework will not handle properly
    I'm genuinely interested in knowing what it is.  I need a demo.
    Please show me the failure.
    
  2. login: "dgp"
  3. mimetype: "text/plain"