Overview
| Artifact ID: | 57efe795f8767fa0fca06c48123757deae9fb2d543412729e3f783d84787dea9 |
|---|---|
| Ticket: | 578155d5a19b348dc1a9fe96cc2c067a59326a89
Very rare bug (segfault) if set variable (with error case) using self-releasable object as new value |
| User & Date: | pooryorick on 2023-03-26 21:39:05 |
Changes
- comment changed to:
I found a very rare but very annoying bug (segfault) that occurs if a variable is set using, for example, Tcl_ObjSetVar2 or similar.<br/> The behaviour stems from the very old check-in [510663a99e3a096bb7bab7314eb59fc805335318] back in 2005.<br/> I had this bug sometimes very, very sporadically, so I executed one of my Tcl services under a debugger until it no-longer occurred. <h2>Explanation:</h2> <ul> <li> <code>Tcl_ObjSetVar2</code>, <code>Tcl_SetVar2Ex</code>, or similar (e. g. with flag <code>TCL_LEAVE_ERR_MSG</code>), is called to set <code>varName</code> to <code>newValue</code>.</li> <li> <code>newValue</code> has a reference count of <code>1</code> since it is referenced somewhere in interpreter state, e.g. a sub-list, sub-dictionary, interp-result, etc. Emphasis on "only once", so <code>newValue->refCount</code> is <code>1</code>.</li> <li> The attempt to set the variable to <code>newValue</code> produces an interim error (for example something going wrong by the resolving of <code>varname</code>, or in trace, etc)</li> <li> The error causes some interp state such as <code>result</code>, <code>errorInfo</code>, <code>errorCode</code>, to be modified, which decrements the reference count of newValue to <code>0</code> and it is deleted, since it happened to be stored in one of those locations.</li> <li> The problem is then the code like here - <a href="http://core.tcl.tk/tcl/artifact/3293a2dbff528bd4?ln=1458">artifact/3293a2dbff528bd4?ln=1458</a> or <a href="http://core.tcl.tk/tcl/artifact/3293a2dbff528bd4?ln=1517">artifact/3293a2dbff528bd4?ln=1517</a>, because it tries to access the already-released newValue. In some cases the code decrements the reference count again, resulting in another attempt to release it.</li> </ul> Why I think, that is a bug? Because only the caller of <code>Tcl_ObjSetVar2</code> really know that the reference of this object should be decremented or not. And because explicit decreasing inside <code>Tcl_ObjSetVar2</code> is very unexpected behavior, IMHO (because rather increased or unmodified).<br/> Otherwise the call of <code>Tcl_ObjSetVar2</code>, <code>Tcl_SetVar2Ex</code> or similar should <b>always</b> look like this: <code><pre> Tcl_IncrRefCount(newValue); Tcl_ObjSetVar2(interp, varName, NULL, newValue, ...) Tcl_DecrRefCount(newValue); </pre></code> But in this case the <a href="http://core.tcl.tk/tcl/artifact/3293a2dbff528bd4?ln=1517">above-mentioned code</a> is totally unnecessary (because newValuePtr->refCount will be never 0).<br/> And then this check-in no longer makes sense at all. So I'm desperate, how it can be fixed plausible resp. totally backwards compatible (this check-in is older as 10 years). Suggestions are welcome...
- login: "pooryorick"
- mimetype: "text/plain"