Skip to content
Tcl Source Code Artifact [57efe795f8]
Login

Artifact [57efe795f8]

Artifact 57efe795f8767fa0fca06c48123757deae9fb2d543412729e3f783d84787dea9:

Ticket change [57efe795f8] - Ticket [578155d5a19b348d|578155d5a1] <i>Very rare bug (segfault) if set variable (with error case) using self-releasable object as new value</i> status still Open with 3 other changes by pooryorick on 2023-03-26 21:39:05.
D 2023-03-26T21:39:05.651
J comment I\sfound\sa\svery\srare\sbut\svery\sannoying\sbug\s(segfault)\sthat\soccurs\sif\sa\svariable\sis\sset\susing,\sfor\sexample,\sTcl_ObjSetVar2\sor\ssimilar.<br/>\r\nThe\sbehaviour\sstems\sfrom\sthe\svery\sold\scheck-in\s[510663a99e3a096bb7bab7314eb59fc805335318]\sback\sin\s2005.<br/>\r\nI\shad\sthis\sbug\ssometimes\svery,\svery\ssporadically,\sso\sI\sexecuted\sone\sof\smy\sTcl\sservices\sunder\sa\sdebugger\suntil\sit\sno-longer\soccurred.\r\n\r\n<h2>Explanation:</h2>\r\n<ul>\r\n<li>\s<code>Tcl_ObjSetVar2</code>,\s<code>Tcl_SetVar2Ex</code>,\sor\ssimilar\s(e.\sg.\swith\sflag\s<code>TCL_LEAVE_ERR_MSG</code>),\sis\scalled\sto\sset\s<code>varName</code>\sto\s<code>newValue</code>.</li>\r\n<li>\s<code>newValue</code>\shas\sa\sreference\scount\sof\s<code>1</code>\ssince\sit\sis\sreferenced\ssomewhere\sin\sinterpreter\sstate,\se.g.\sa\ssub-list,\ssub-dictionary,\sinterp-result,\setc.\sEmphasis\son\s"only\sonce",\sso\s<code>newValue->refCount</code>\sis\s<code>1</code>.</li>\r\n<li>\sThe\sattempt\sto\sset\sthe\svariable\sto\s<code>newValue</code>\sproduces\san\sinterim\serror\s(for\sexample\ssomething\sgoing\swrong\sby\sthe\sresolving\sof\s<code>varname</code>,\sor\sin\strace,\setc)</li>\r\n<li>\sThe\serror\scauses\ssome\sinterp\sstate\ssuch\sas\s<code>result</code>,\s<code>errorInfo</code>,\s<code>errorCode</code>,\sto\sbe\smodified,\swhich\sdecrements\sthe\sreference\scount\sof\snewValue\sto\s<code>0</code>\sand\sit\sis\sdeleted,\ssince\sit\shappened\sto\sbe\sstored\sin\sone\sof\sthose\slocations.</li>\r\n<li>\sThe\sproblem\sis\sthen\sthe\scode\slike\shere\s-\s<a\shref="http://core.tcl.tk/tcl/artifact/3293a2dbff528bd4?ln=1458">artifact/3293a2dbff528bd4?ln=1458</a>\sor\s<a\shref="http://core.tcl.tk/tcl/artifact/3293a2dbff528bd4?ln=1517">artifact/3293a2dbff528bd4?ln=1517</a>,\sbecause\sit\stries\sto\saccess\sthe\salready-released\snewValue.\s\sIn\ssome\scases\sthe\scode\sdecrements\sthe\sreference\scount\sagain,\sresulting\sin\sanother\sattempt\sto\srelease\sit.</li>\r\n</ul>\r\n\r\nWhy\sI\sthink,\sthat\sis\sa\sbug?\r\n\r\nBecause\sonly\sthe\scaller\sof\s<code>Tcl_ObjSetVar2</code>\sreally\sknow\sthat\sthe\sreference\sof\sthis\sobject\sshould\sbe\sdecremented\sor\snot.\sAnd\sbecause\sexplicit\sdecreasing\sinside\s<code>Tcl_ObjSetVar2</code>\sis\svery\sunexpected\sbehavior,\sIMHO\s(because\srather\sincreased\sor\sunmodified).<br/>\r\nOtherwise\sthe\scall\sof\s<code>Tcl_ObjSetVar2</code>,\s<code>Tcl_SetVar2Ex</code>\sor\ssimilar\sshould\s<b>always</b>\slook\slike\sthis:\r\n<code><pre>\r\nTcl_IncrRefCount(newValue);\r\nTcl_ObjSetVar2(interp,\svarName,\sNULL,\snewValue,\s...)\r\nTcl_DecrRefCount(newValue);\r\n</pre></code>\r\nBut\sin\sthis\scase\sthe\s<a\shref="http://core.tcl.tk/tcl/artifact/3293a2dbff528bd4?ln=1517">above-mentioned\scode</a>\sis\stotally\sunnecessary\s(because\snewValuePtr->refCount\swill\sbe\snever\s0).<br/>\r\nAnd\sthen\sthis\scheck-in\sno\slonger\smakes\ssense\sat\sall.\r\n\r\nSo\sI'm\sdesperate,\show\sit\scan\sbe\sfixed\splausible\sresp.\stotally\sbackwards\scompatible\s(this\scheck-in\sis\solder\sas\s10\syears).\s\r\n\r\nSuggestions\sare\swelcome...
J login pooryorick
J mimetype text/plain
K 578155d5a19b348dc1a9fe96cc2c067a59326a89
U pooryorick
Z 3902bbc5c972fc6986302058f1c515aa