Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 9793890404222f63ec056fda0e8aa3f8949aeed3702f567185c75c96b7c3d954
Ticket: 578155d5a19b348dc1a9fe96cc2c067a59326a89
Very rare bug (segfault) if set variable (with error case) using self-releasable object as new value
User & Date: sebres on 2018-01-16 09:46:10
Changes

  1. icomment:
    > I don't know of any counterexample, and you have not given me one.
    
    But, I had.
    So I'll try to explain my example with function X again.
    
    Now the developer has several issues depending from initial situation:
    
    1. If X1 "owns" the object reference (caller of X1 transferred object to X1), we should always
    write the code like below:
    ```
    int X1 (Tcl_Obj *varName, Tcl_Obj *objPtr, ...) {
      Tcl_Obj *varObj;
      int orgRefCount = objPtr->refCount;
      if ((varObj = Tcl_ObjSetVar2(interp, varName, NULL, objPtr, flags)) == NULL) {
        if (orgRefCount != 0) {
          /* BUG-X1.1 - can be wrong here, because the object can be destroyed in trace */
          Tcl_DecrRefCount(objPtr);
        }
        /* Handle error ... */
        return TCL_ERROR;
      }
      if (varObj == objPtr) {
        if (orgRefCount != 0) {
          Tcl_DecrRefCount(objPtr);
        }
      } else {
        /* BUG-X1.2 - can be dangerous here, because the object can be destroyed in trace */
        if (orgRefCount != 0 && objPtr->refCount > 1) {
          Tcl_DecrRefCount(objPtr);
        }
      }
    }
    ```
    
    2. If X2 don't "own" the object (caller of X2 still "holds" the ownership (also indirectly, 
    e. g. as member of class), it means - just assign a variable), we should always write the 
    code like below:
    
    ```
    int X2 (Tcl_Obj *varName, Tcl_Obj *objPtr, ...) {
    
      if (sharingAllowed) {
    
        Tcl_IncrRefCount(objPtr);
        if (Tcl_ObjSetVar2(interp, varName, NULL, objPtr, flags) == NULL) {
          /* BUG-X2.1 - it can produce a leak, if objPtr->refCount was decremented to 1 during 
             some trace-execution, but if we'll do it always, we can unwanted destroy object */
          if (objPtr->refCount > 1) {
            Tcl_DecrRefCount(objPtr);
          }     
          /* Handle error ... */
          return TCL_ERROR;
        }
        /* BUG-X2.2 - it can produce a leak, if objPtr->refCount was decremented to 1 during 
           some trace-execution, but if we'll do it always, we can unwanted destroy object */
        if (objPtr->refCount > 1) {
           Tcl_DecrRefCount(objPtr);
        }
        /* Handle success case ... */
        return TCL_OK;
    
      } else {
    
        if (!objPtr->refCount) {
          /* BUG-X2.3 - Impossible, we should increment it before call of Tcl_ObjSetVar2, 
             but in this case the object can get shared state after assignment to var */
        }
        if (Tcl_ObjSetVar2(interp, varName, NULL, objPtr, flags) == NULL) {
          /* Handle error ... */
          /* BUG-X2.4 - the object can be destroyed inside Tcl_ObjSetVar2, so not allowed to use anymore 
             in caller of X2 */
          return TCL_ERROR;
        }
    
      }
    }
    ```
    
  2. login: "sebres"
  3. mimetype: "text/plain"