Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 7775529f8f7a4b64f8091279f28ba78caf5c8be04fbb2d3a02ba7a4c13771c4b
Ticket: 578155d5a19b348dc1a9fe96cc2c067a59326a89
Very rare bug (segfault) if set variable (with error case) using self-releasable object as new value
User & Date: sebres on 2023-03-31 13:05:10
Changes

  1. closedate changed to: "2460035.04525838"
  2. closer changed to: "sebres"
  3. icomment:
    > _"we can be not sure what happens with reference in Tcl_ObjSetVar2"_
    
    >>    _Yes, we can be sure: The reference count of the Tcl_Obj is incremented, and in the failure case, it is decremented._
    
    No, indirectly it is not that what you assume here - it does decrement only if it is the "last" reference (to free the object), see it again the criticized fix [510663a99e3a096](https://core.tcl-lang.org/tcl/info/510663a99e3a096bb7bab7314eb59fc805335318):
    ```
        } else if (dictPtr->refCount == 0) {
    	Tcl_DecrRefCount(dictPtr);
        }
    ```
    and others inclusive this one in tclptrsetvar
    ```
      earlyError:
        if (newValuePtr->refCount == 0) {
    	Tcl_DecrRefCount(newValuePtr);
        }
        goto cleanup;
    ```
    or in the [current code](https://core.tcl-lang.org/tcl/file?ci=tip&name=generic/tclVar.c&ln=1864-1866).
    
    > _"Much worse it looks just like undefined behavior."_
    
    >>    _False. Tcl_ObjSetVar2 performs the equivalent of incrementing the variable, and then decrementing it in the error case._
    
    No, as already illustrated it is not. 
    
    Again, I don't complain the increment and decrement, but a **destructive decrement**, what is not expected in error case. The is more or less a matter of the API responsibility, and a nominal condition of every function in error case (here value is not assigned to variable, dict or whatever) is don't touch the value, still worse to remove it on certain conditions (by default). I'd say nothing if the decrement would be non-destructive (e. g. <code>newValuePtr->refCount--</code>), so wouldn't free the object, but it does. Und exactly that I called as it were like UB.
    
    > _"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..."_
    
    >>    _False. The caller either increments the Tcl_Obj prior to passing it to Tcl_ObjSetVar2 and arranges for it to be decremented later, or just doesn't increment it and allows Tcl_ObjSetVar2 to manage it._
    
    But exactly that is hardly possible on certain conditions as I showed in example before. Because the decrement takes place conditionally and object gets destroyed or even not, depending on its reference counter.
    
    > _"Thus either he do this nevertheless and can get segfault, or he produces intentionally a memory leak (quasi accepts the risk of possible leak)."_
    
    >>    _False. If the caller wants to use the Tcl_Obj after passsing it to Tcl_ObjSetVar2, it simply increments the reference count first, and then decrements it later, or passes it back to its own caller, explaining that the reference count has been incremented._
    
    But if one is forced always to increment the value before <code>Tcl_ObjSetVar2</code> why the trick (with decrement on refCount == 0) is necessary at all?
    Again, I don't complain about that. My expectations are that in error case the object doesn't become destroyed.
    
    ...
    
    >> _The example failed to make use of the result of Tcl_ObjSetVar2. As the documentation warns, it is not correct to simply return objPtr becuause the Tcl_Obj * returned by Tcl_ObjSetVar2 may be an entirely different object. The correct code would look like this:_
    
    >> <i><pre><code>Tcl_Obj * CacheSetObj(Tcl_Obj *varName, Tcl_Obj *objPtr)
    {
      return Tcl_ObjSetVar2(interp, cache, varName, objPtr, 0);</code>
    }</code></pre></i>
    
    >> ...
    
    Your code example does completely different thing - now it is able to return NULL, whether my code **example** (I emphasize example) always returns an object. The assignment to variable is here just a caching mechanism which can help to avoid retrieve object again and again, but it'd work if cache is unavailable or whatever is happening with caching variable.
    
    Again, the usage of the object after assignment is not infrequently the case and this was just a simplest example I could imagine.
    Sure one can use increment before one tries to store the object to variable, but as already several times said, normally it should not be a mandatory condition at all. 
    Let alone sometimes one would need to have unshared object (refCount < 2) for instance to retain it alterable, otherwise one'd start the dancing with tambourine (if shared duplicate etc pp).
    
    >> _Notice that there is no reason for CacheSetObj to manipulate the reference count of objPtr at all, and that Tcl_ObjSetVar2 correctly cleans up objPtr if needed._
    
    You seem don't understand the code and the intention at all.
    In that example it should not clean the objPtr, never ever! Moreover set to var (assign to dict, etc) is **totaly secondary action** here, that only allows to cache (and still retain unshared) object which gets processed further.
    
    However I have no intention to convince you or the TCT.<br/>
    Bye.
    
  4. login: "sebres"
  5. mimetype: "text/x-markdown"