<i>Very rare bug (segfault) if set variable (with error case) using self-releasable object as new value</i> status still Pending with 3 other changes
by
sebres
2023-03-29 12:51:52.
D 2023-03-29T12:51:52.462
J icomment Just\sfor\sthe\srecord,\shere\sis\ssimplest\sexample\sto\sillustrate\sthe\sissue:\r\n<code><pre>\r\nTcl_Obj\s*\sCacheSetObj(Tcl_Obj\s*varName,\sTcl_Obj\s*objPtr)\r\n{\r\n\s\sTcl_ObjSetVar2(interp,\scache,\svarName,\sobjPtr,\s0);\r\n\s\sreturn\sobjPtr;\r\n}\r\n\r\n...\r\n\r\nif\s((objPtr\s=\sCacheGetObj(someName))\s!=\sNULL)\r\n\s\sreturn\sobjPtr;\r\nreturn\sCacheSetObj(someName,\sSomethingToObtainObj(...));\r\n</pre></code>\r\n\r\nThis\sis\scompletely\svalid\sscenario\sand\sit'd\seven\swork\salmost\salways\sproperly...\sunless\s<code>SomethingToObtainObj</code>\sreturns\sunreferenced\sobject\sand\scache\sdoesn't\sexists\sfor\ssome\sreason,\sbecause\sthen\safter\sfailed\sassignment\sthe\sobject\sgets\sdeleted.\r\n\r\nSimilar\sscenarios\s(like\stry\sto\sset\sto\svar1,\sif\sfailed\sto\svar2/dict/whatever)\sare\salso\spretty\simaginable.\r\nAs\swell\sas\svise\sversa\ssituation\s(unwillingness\sto\suse\sthe\sobject\safter\sset\svariable):\snecessity\sto\sfree\sthe\sobject\sin\serror\scase\s(but\sconditionally)\smakes\sit\shardly\spossible\stoo,\sbecause\sone\sdoesn't\sreally\sknow\sthe\sstate\sof\sobject\s(whether\sit\salready\sremoved\sor\snot).\r\n\r\nAnd\sagain\san\sincrement\sand\sdecrement\saround\sis\snot\sreally\sa\ssolution\shere,\sbecause:\r\n<ul>\r\n<li>it\sis\stotally\sunneeded\saction</li>\r\n<li>it\sis\snot\sobvious\sfor\sthe\scaller\s(basically\sthe\smatter\sof\sresponsibility\sof\sAPI\sfunctions\sand\ssolves\san\sinconsistency\sintroduced\sin\sTcl\sAPI)<li>\r\n<li>it\scannot\sbe\sseriously\sconsidered\sas\smandatory\smerely\son\sthe\sgrounds\sthat\sthe\s"fix"\s[510663a99e3a096bb7bab7314eb59fc805335318]\sbecomes\stotally\sunnecessary\s(if\sone\sassumes\sonly\sto\suse\s<code>Tcl_ObjSetVar2</code>\swith\sobjects\swith\srefCount\s>\s0)</li>.\r\n</ul>\r\n\r\nRather\sTcl\sneeds\ssome\snew\s(may\sbe\sinternal)\sfunction\sworking\swith\ssuch\sa\sdecrement,\sor\sa\sflag\s(like\s<code>TCL_OWN_OBJREF</code>)\sallowing\sthe\sdecrement\sin\serror\scase.\r\n\r\nAnd\sinconsistent\s"fix"\s[510663a99e3a096bb7bab7314eb59fc805335318]\sof\s<code>TclPtrSetVar</code>\swith\s<code>earlyError</code>\sblock\smust\sbe\sdefinitely\sreverted,\smerely\sfor\sthe\sreason\sthat\s<b>a\spotential\sfor\san\sunlikely\sleak\sis\smany\stimes\sbetter\sthan\san\sunlikely\sheisenbug\s(double\sfree,\susage\safter\sfree,\sreference\serror\setc)</b>,\sespecially\sif\sa\sleak,\sthat\smay\sarise\sfor\shabitude\sreasons,\soccurs\sonly\sin\serror\scase.
J login sebres
J mimetype text/x-fossil-wiki
K 578155d5a19b348dc1a9fe96cc2c067a59326a89
U sebres
Z 5fb695586d6644bc2cf03eb8672d1c99