Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: cab3995fba5aabc7b6f0ad6ef738e1dde673ff2767ddc06b2a278478722e8dfa
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-11 22:11:35
Changes

  1. icomment:
    > 1. That's false.  Look at the code.
    
    And?... 
    
    1.a. You are also a bit wrong here, because there are more places, where it can be 
        (also during set, see https://core.tcl.tk/tcl/artifact/1f16d4aba6?ln=1934-1936).
    1.b. You'll say that TclObjLookupVarEx can't cause decrement of reference of some object? It can! 
        (Think about the traces on get, in-place changes of internal representation of varname, etc.)
    1.c. The caller of Tcl_SetVar2Ex don't know at all why this function was failed, during lookup or during 
        some trace call through set... Or something else. So what should he do???
    
    > 2.a. Here's the framework for a caller to cover all the cases.
    
    This scenario is well known to me and may pretty good work for simply (especially for new-created objects) 
    or for the objects for which you can sure increment reference before you assign it.
    
    But it does not good for foreign objects, which you don't "owns" resp. for which you must not 
    call Tcl_IncrRefCount, for example because you must not make it shared in this case (should be 
    definitely "unshared" by assignment) or because you can not be sure that assignment fails, so 
    your decrement of the refCount hereafter will guaranteed destroy this foreign object.
    
    You forget that the variable is not necessary a simple var, it can be "upvared" to 
    namespace (class/object) member, linked via some "complex" resolver to some scope- or 
    scope-less frame, etc.
    
    > 2.b. Please show me the failure.
    
    Ok, here you go: just imagine, that you are developer of function X (e. g. with public API) that looks like:
    
    int X (Tcl_Obj *varName, Tcl_Obj *objPtr, ...);
    
    You don't know what the object "objPtr" is (it is not yours, you'll get it from foreign code and from other people),
    so it can have refCount == 0, 1 and larger as 1.
    How you can set it to the variable varName, inside this function and what you'll do in error case.
    If you'll increment it, possibly you can (unwanted) destroy it in error case by the paired decrement.
    If you don't increment it, you can not be sure what do you want to do in the error case.
    Also extra sharing of the assignment-object may be sometimes undesirable in many-many cases. At least can cause many
    unwanted interim duplication's (just think about, that by assignment, the trace on set can be better executed with
    unshared object, etc.). 
    But in worse case may even cause errors like "...something... called with shared object" or similar).
    
    Of course, I can also imagine some scenario's (like yours framework) for all this cases, BUT then this essentially
    trivial task (just "set a var") will grow in no more trivial use-case... (and more code - more errors, at least by
    people that are not very familiar with Tcl-API, so you'll really force them always to write all this around it?).
    
    The question is WHY it should be?! So why it all should be necessary just to assign an object to the variable?
    
    This (in my opinion controversial) behavior can be easy "fixed":
    
    - either using special flag (like suggested below) - TCL_OWN_OBJREF, which will always decrease the refCount in error
      case (so the caller can control it resp. does the decision what should happen with him);
    - or using pre-checking of refCount inside set-functions (before variable-lookup resp. assignment takes place, and
      before the object gets no more sane state in undefined error case).
    - or even both to be totally backwards-compatible in all cases and to provide for developers the opportunity to decide,
      what should happen.
    
    Meanwhile I've implemented both things for my own tcl-fork, and it works fine and saves me 
    many time, work and code-lines (not to mention the readability).
    
  2. login: "sebres"
  3. mimetype: "text/plain"