<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
.
D 2018-01-11T20:24:10.264
J icomment >\s\s\s1)\sThe\sfunction\schecks\sthe\srefCount\sonly\sAFTER\strying\sof\sset\sthe\svariable\r\n>\s(which\scan\schange\sthe\srefCount\sof\sthe\sobject\sby\scalling\sof\sthe\straces,\setc).\r\n\r\nThat's\sfalse.\s\sLook\sat\sthe\scode.\r\n\r\nhttps://core.tcl.tk/tcl/artifact/1f16d4aba6?ln=1718-1728\r\n\r\n\s\s1718\s\s\s\s\s\svarPtr\s=\sTclObjLookupVarEx(interp,\spart1Ptr,\spart2Ptr,\sflags,\s"set",\r\n\s\s1719\s\s\t\s\s\s\s/*createPart1*/\s1,\s/*createPart2*/\s1,\s&arrayPtr);\r\n\s\s1720\s\s\s\s\s\sif\s(varPtr\s==\sNULL)\s{\r\n\s\s1721\s\s\tif\s(newValuePtr->refCount\s==\s0)\s{\r\n\s\s1722\s\s\t\s\s\s\sTcl_DecrRefCount(newValuePtr);\r\n\s\s1723\s\s\t}\r\n\s\s1724\s\s\treturn\sNULL;\r\n\s\s1725\s\s\s\s\s\s}\r\n\s\s1726\s\s\r\n\s\s1727\s\s\s\s\s\sreturn\sTclPtrSetVarIdx(interp,\svarPtr,\sarrayPtr,\spart1Ptr,\spart2Ptr,\r\n\s\s1728\s\s\t\s\s\s\snewValuePtr,\sflags,\s-1);\r\n\r\nThe\sDecrement\scomes\sonly\safter\sa\sfailed\slookup\sattempt,\swhich\scalls\sno\straces.\r\nTraces\sare\scalled\sfrom\swithin\sTclPtrSetVarIdx().\sThe\slookup\sroutine\salso\r\ndoesn't\stake\snewValuePtr\sas\san\sargument.\sThe\sonly\s"callback"\sopportunities\r\nI\ssee\sin\sthe\slookup\sroutines\sis\sthe\sinvocation\sof\svariable\sresolvers.\r\n\r\n>\s2)\sFor\sthe\sdeveloper\susing\sTcl_ObjSetVar,\sit\sis\svery\sbad\sup\sto\simpossible\r\n>\sto\sact\splausible\sin\serror\scase,\sbecause\she\sdon't\sknow\swhen\she\sshould\sdecrease\r\n>\srefCount\sin\serror\scase\sor\snot,\s\r\n\r\nHere's\sthe\sframework\sfor\sa\scaller\sto\scover\sall\sthe\scases.\r\nIf\sthe\scaller\swants\sto\sprevent\sany\sfree\sof\s"value"\sby\sTcl_ObjSetVar2(),\r\nit\scan\scall\sTcl_IncrRefCount(value)\sbefore\scalling\sTcl_ObjSetVar2()\r\nand\scall\sTcl_DecrRefCount(value)\safter\sit\sno\slonger\swill\suse\s"value".\r\nThis\swill\smanage\sthe\slifetime\swithout\sleaks.\s\sIf\sthe\scaller\shas\sno\sneed\r\nto\suse\s"value"\sother\sthan\spassing\sit\sto\sTcl_ObjSetVar2(),\sthen\sthe\scode\r\nbelow\sdoes\sit.\r\n\r\nTcl_Obj\s*value;\s/*\sEither\screate\sor\sreceive\sas\sargument,\sany\srefcount.\s*/\r\nTcl_Obj\s*stored;\r\nTcl_Obj\s*varName\s=\sTcl_NewStringObj("foo",\s-1);\r\nTcl_IncrRefCount(varName);\r\nstored\s=\sTcl_ObjSetVar2(interp,\svarName,\sNULL,\svalue,\sTCL_LEAVE_ERR_MSG);\r\nTcl_DecrRefCount(varName);\r\nif\s(stored\s==\sNULL)\s{\r\n\s\s\s\s/*\sIf\svalue\shad\srefcount\s0,\sit\shas\sbeen\sfreed.\s*/\r\n\s\s\s\s/*\sOtherwise,\sit\sstill\shas\sthe\srefcount\sit\swas\scalled\swith.\s*/\r\n}\selse\sif\s(stored\s==\svalue)\s{\r\n\s\s\s\s/*\sThe\svariable\s"foo"\sholds\svalue\s*/\r\n\s\s\s\s/*\sThe\srefcount\sof\svalue\sone\sgreater\sthan\sit\swas\scalled\swith.\s*/\r\n}\selse\s{\r\n\s\s\s\s/*\sThe\svariable\s"foo"\sholds\sstored,\swhich\sis\sdifferent\sfrom\svalue.\s*/\r\n\s\s\s\s/*\sTraces\sintervened.\s*/\r\n\s\s\s\s/*\sIf\svalue\shad\srefcount\s0,\sit\sis\sfreed.\s*/\r\n\s\s\s\s/*\sOtherwise,\sit\sstill\shas\sthe\srefcount\sit\swas\scalled\swith.\s*/\r\n}\r\n\r\nThis\sapproach\sis\svery\sconvenient\sfor\scallers.\sIn\smost\scases\sthey\r\nneed\snot\sdeal\swith\srefcounts\sof\s"value"\sat\sall.\r\n\r\nIf\sthere's\ssome\sscenario\sthat\sframework\swill\snot\shandle\sproperly\r\nI'm\sgenuinely\sinterested\sin\sknowing\swhat\sit\sis.\s\sI\sneed\sa\sdemo.\r\nPlease\sshow\sme\sthe\sfailure.
J login dgp
J mimetype text/plain
K 578155d5a19b348dc1a9fe96cc2c067a59326a89
U dgp
Z 344e25ca773875a6c7dc6b1a73ac85fb