<i>Very rare bug (segfault) if set variable (with error case) using self-releasable object as new value</i> status still Closed with 5 other changes
by
.
D 2023-03-31T13:05:10.324
J closedate 2460035.04525838
J closer sebres
J icomment >\s_"we\scan\sbe\snot\ssure\swhat\shappens\swith\sreference\sin\sTcl_ObjSetVar2"_\r\n\r\n>>\s\s\s\s_Yes,\swe\scan\sbe\ssure:\sThe\sreference\scount\sof\sthe\sTcl_Obj\sis\sincremented,\sand\sin\sthe\sfailure\scase,\sit\sis\sdecremented._\r\n\r\nNo,\sindirectly\sit\sis\snot\sthat\swhat\syou\sassume\shere\s-\sit\sdoes\sdecrement\sonly\sif\sit\sis\sthe\s"last"\sreference\s(to\sfree\sthe\sobject),\ssee\sit\sagain\sthe\scriticized\sfix\s[510663a99e3a096](https://core.tcl-lang.org/tcl/info/510663a99e3a096bb7bab7314eb59fc805335318):\r\n```\r\n\s\s\s\s}\selse\sif\s(dictPtr->refCount\s==\s0)\s{\r\n\tTcl_DecrRefCount(dictPtr);\r\n\s\s\s\s}\r\n```\r\nand\sothers\sinclusive\sthis\sone\sin\stclptrsetvar\r\n```\r\n\s\searlyError:\r\n\s\s\s\sif\s(newValuePtr->refCount\s==\s0)\s{\r\n\tTcl_DecrRefCount(newValuePtr);\r\n\s\s\s\s}\r\n\s\s\s\sgoto\scleanup;\r\n```\r\nor\sin\sthe\s[current\scode](https://core.tcl-lang.org/tcl/file?ci=tip&name=generic/tclVar.c&ln=1864-1866).\r\n\r\n>\s_"Much\sworse\sit\slooks\sjust\slike\sundefined\sbehavior."_\r\n\r\n>>\s\s\s\s_False.\sTcl_ObjSetVar2\sperforms\sthe\sequivalent\sof\sincrementing\sthe\svariable,\sand\sthen\sdecrementing\sit\sin\sthe\serror\scase._\r\n\r\nNo,\sas\salready\sillustrated\sit\sis\snot.\s\r\n\r\nAgain,\sI\sdon't\scomplain\sthe\sincrement\sand\sdecrement,\sbut\sa\s**destructive\sdecrement**,\swhat\sis\snot\sexpected\sin\serror\scase.\sThe\sis\smore\sor\sless\sa\smatter\sof\sthe\sAPI\sresponsibility,\sand\sa\snominal\scondition\sof\severy\sfunction\sin\serror\scase\s(here\svalue\sis\snot\sassigned\sto\svariable,\sdict\sor\swhatever)\sis\sdon't\stouch\sthe\svalue,\sstill\sworse\sto\sremove\sit\son\scertain\sconditions\s(by\sdefault).\sI'd\ssay\snothing\sif\sthe\sdecrement\swould\sbe\snon-destructive\s(e.\sg.\s<code>newValuePtr->refCount--</code>),\sso\swouldn't\sfree\sthe\sobject,\sbut\sit\sdoes.\sUnd\sexactly\sthat\sI\scalled\sas\sit\swere\slike\sUB.\r\n\r\n>\s_"For\sthe\sdeveloper\susing\sTcl_ObjSetVar,\sit\sis\svery\sbad\sup\sto\simpossible\sto\sact\splausible\sin\serror\scase,\sbecause\she\sdon't\sknow\swhen\she\sshould\sdecrease\srefCount\sin\serror\scase\sor\snot..."_\r\n\r\n>>\s\s\s\s_False.\sThe\scaller\seither\sincrements\sthe\sTcl_Obj\sprior\sto\spassing\sit\sto\sTcl_ObjSetVar2\sand\sarranges\sfor\sit\sto\sbe\sdecremented\slater,\sor\sjust\sdoesn't\sincrement\sit\sand\sallows\sTcl_ObjSetVar2\sto\smanage\sit._\r\n\r\nBut\sexactly\sthat\sis\shardly\spossible\son\scertain\sconditions\sas\sI\sshowed\sin\sexample\sbefore.\sBecause\sthe\sdecrement\stakes\splace\sconditionally\sand\sobject\sgets\sdestroyed\sor\seven\snot,\sdepending\son\sits\sreference\scounter.\r\n\r\n>\s_"Thus\seither\she\sdo\sthis\snevertheless\sand\scan\sget\ssegfault,\sor\she\sproduces\sintentionally\sa\smemory\sleak\s(quasi\saccepts\sthe\srisk\sof\spossible\sleak)."_\r\n\r\n>>\s\s\s\s_False.\sIf\sthe\scaller\swants\sto\suse\sthe\sTcl_Obj\safter\spasssing\sit\sto\sTcl_ObjSetVar2,\sit\ssimply\sincrements\sthe\sreference\scount\sfirst,\sand\sthen\sdecrements\sit\slater,\sor\spasses\sit\sback\sto\sits\sown\scaller,\sexplaining\sthat\sthe\sreference\scount\shas\sbeen\sincremented._\r\n\r\nBut\sif\sone\sis\sforced\salways\sto\sincrement\sthe\svalue\sbefore\s<code>Tcl_ObjSetVar2</code>\swhy\sthe\strick\s(with\sdecrement\son\srefCount\s==\s0)\sis\snecessary\sat\sall?\r\nAgain,\sI\sdon't\scomplain\sabout\sthat.\sMy\sexpectations\sare\sthat\sin\serror\scase\sthe\sobject\sdoesn't\sbecome\sdestroyed.\r\n\r\n...\r\n\r\n>>\s_The\sexample\sfailed\sto\smake\suse\sof\sthe\sresult\sof\sTcl_ObjSetVar2.\sAs\sthe\sdocumentation\swarns,\sit\sis\snot\scorrect\sto\ssimply\sreturn\sobjPtr\sbecuause\sthe\sTcl_Obj\s*\sreturned\sby\sTcl_ObjSetVar2\smay\sbe\san\sentirely\sdifferent\sobject.\sThe\scorrect\scode\swould\slook\slike\sthis:_\r\n\r\n>>\s<i><pre><code>Tcl_Obj\s*\sCacheSetObj(Tcl_Obj\s*varName,\sTcl_Obj\s*objPtr)\r\n{\r\n\s\sreturn\sTcl_ObjSetVar2(interp,\scache,\svarName,\sobjPtr,\s0);</code>\r\n}</code></pre></i>\r\n\r\n>>\s...\r\n\r\nYour\scode\sexample\sdoes\scompletely\sdifferent\sthing\s-\snow\sit\sis\sable\sto\sreturn\sNULL,\swhether\smy\scode\s**example**\s(I\semphasize\sexample)\salways\sreturns\san\sobject.\sThe\sassignment\sto\svariable\sis\shere\sjust\sa\scaching\smechanism\swhich\scan\shelp\sto\savoid\sretrieve\sobject\sagain\sand\sagain,\sbut\sit'd\swork\sif\scache\sis\sunavailable\sor\swhatever\sis\shappening\swith\scaching\svariable.\r\n\r\nAgain,\sthe\susage\sof\sthe\sobject\safter\sassignment\sis\snot\sinfrequently\sthe\scase\sand\sthis\swas\sjust\sa\ssimplest\sexample\sI\scould\simagine.\r\nSure\sone\scan\suse\sincrement\sbefore\sone\stries\sto\sstore\sthe\sobject\sto\svariable,\sbut\sas\salready\sseveral\stimes\ssaid,\snormally\sit\sshould\snot\sbe\sa\smandatory\scondition\sat\sall.\s\r\nLet\salone\ssometimes\sone\swould\sneed\sto\shave\sunshared\sobject\s(refCount\s<\s2)\sfor\sinstance\sto\sretain\sit\salterable,\sotherwise\sone'd\sstart\sthe\sdancing\swith\stambourine\s(if\sshared\sduplicate\setc\spp).\r\n\r\n>>\s_Notice\sthat\sthere\sis\sno\sreason\sfor\sCacheSetObj\sto\smanipulate\sthe\sreference\scount\sof\sobjPtr\sat\sall,\sand\sthat\sTcl_ObjSetVar2\scorrectly\scleans\sup\sobjPtr\sif\sneeded._\r\n\r\nYou\sseem\sdon't\sunderstand\sthe\scode\sand\sthe\sintention\sat\sall.\r\nIn\sthat\sexample\sit\sshould\snot\sclean\sthe\sobjPtr,\snever\sever!\sMoreover\sset\sto\svar\s(assign\sto\sdict,\setc)\sis\s**totaly\ssecondary\saction**\shere,\sthat\sonly\sallows\sto\scache\s(and\sstill\sretain\sunshared)\sobject\swhich\sgets\sprocessed\sfurther.\r\n\r\nHowever\sI\shave\sno\sintention\sto\sconvince\syou\sor\sthe\sTCT.<br/>\r\nBye.
J login sebres
J mimetype text/x-markdown
K 578155d5a19b348dc1a9fe96cc2c067a59326a89
U sebres
Z af2a3bde5b15e5584ecfd6fdadb2a71c