<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-11T22:11:35.204
J icomment >\s1.\sThat's\sfalse.\s\sLook\sat\sthe\scode.\r\n\r\nAnd?...\s\r\n\r\n1.a.\sYou\sare\salso\sa\sbit\swrong\shere,\sbecause\sthere\sare\smore\splaces,\swhere\sit\scan\sbe\s\r\n\s\s\s\s(also\sduring\sset,\ssee\shttps://core.tcl.tk/tcl/artifact/1f16d4aba6?ln=1934-1936).\r\n1.b.\sYou'll\ssay\sthat\sTclObjLookupVarEx\scan't\scause\sdecrement\sof\sreference\sof\ssome\sobject?\sIt\scan!\s\r\n\s\s\s\s(Think\sabout\sthe\straces\son\sget,\sin-place\schanges\sof\sinternal\srepresentation\sof\svarname,\setc.)\r\n1.c.\sThe\scaller\sof\sTcl_SetVar2Ex\sdon't\sknow\sat\sall\swhy\sthis\sfunction\swas\sfailed,\sduring\slookup\sor\sduring\s\r\n\s\s\s\ssome\strace\scall\sthrough\sset...\sOr\ssomething\selse.\sSo\swhat\sshould\she\sdo???\r\n\r\n>\s2.a.\sHere's\sthe\sframework\sfor\sa\scaller\sto\scover\sall\sthe\scases.\r\n\r\nThis\sscenario\sis\swell\sknown\sto\sme\sand\smay\spretty\sgood\swork\sfor\ssimply\s(especially\sfor\snew-created\sobjects)\s\r\nor\sfor\sthe\sobjects\sfor\swhich\syou\scan\ssure\sincrement\sreference\sbefore\syou\sassign\sit.\r\n\r\nBut\sit\sdoes\snot\sgood\sfor\sforeign\sobjects,\swhich\syou\sdon't\s"owns"\sresp.\sfor\swhich\syou\smust\snot\s\r\ncall\sTcl_IncrRefCount,\sfor\sexample\sbecause\syou\smust\snot\smake\sit\sshared\sin\sthis\scase\s(should\sbe\s\r\ndefinitely\s"unshared"\sby\sassignment)\sor\sbecause\syou\scan\snot\sbe\ssure\sthat\sassignment\sfails,\sso\s\r\nyour\sdecrement\sof\sthe\srefCount\shereafter\swill\sguaranteed\sdestroy\sthis\sforeign\sobject.\r\n\r\nYou\sforget\sthat\sthe\svariable\sis\snot\snecessary\sa\ssimple\svar,\sit\scan\sbe\s"upvared"\sto\s\r\nnamespace\s(class/object)\smember,\slinked\svia\ssome\s"complex"\sresolver\sto\ssome\sscope-\sor\s\r\nscope-less\sframe,\setc.\r\n\r\n>\s2.b.\sPlease\sshow\sme\sthe\sfailure.\r\n\r\nOk,\shere\syou\sgo:\sjust\simagine,\sthat\syou\sare\sdeveloper\sof\sfunction\sX\s(e.\sg.\swith\spublic\sAPI)\sthat\slooks\slike:\r\n\r\nint\sX\s(Tcl_Obj\s*varName,\sTcl_Obj\s*objPtr,\s...);\r\n\r\nYou\sdon't\sknow\swhat\sthe\sobject\s"objPtr"\sis\s(it\sis\snot\syours,\syou'll\sget\sit\sfrom\sforeign\scode\sand\sfrom\sother\speople),\r\nso\sit\scan\shave\srefCount\s==\s0,\s1\sand\slarger\sas\s1.\r\nHow\syou\scan\sset\sit\sto\sthe\svariable\svarName,\sinside\sthis\sfunction\sand\swhat\syou'll\sdo\sin\serror\scase.\r\nIf\syou'll\sincrement\sit,\spossibly\syou\scan\s(unwanted)\sdestroy\sit\sin\serror\scase\sby\sthe\spaired\sdecrement.\r\nIf\syou\sdon't\sincrement\sit,\syou\scan\snot\sbe\ssure\swhat\sdo\syou\swant\sto\sdo\sin\sthe\serror\scase.\r\nAlso\sextra\ssharing\sof\sthe\sassignment-object\smay\sbe\ssometimes\sundesirable\sin\smany-many\scases.\sAt\sleast\scan\scause\smany\r\nunwanted\sinterim\sduplication's\s(just\sthink\sabout,\sthat\sby\sassignment,\sthe\strace\son\sset\scan\sbe\sbetter\sexecuted\swith\r\nunshared\sobject,\setc.).\s\r\nBut\sin\sworse\scase\smay\seven\scause\serrors\slike\s"...something...\scalled\swith\sshared\sobject"\sor\ssimilar).\r\n\r\nOf\scourse,\sI\scan\salso\simagine\ssome\sscenario's\s(like\syours\sframework)\sfor\sall\sthis\scases,\sBUT\sthen\sthis\sessentially\r\ntrivial\stask\s(just\s"set\sa\svar")\swill\sgrow\sin\sno\smore\strivial\suse-case...\s(and\smore\scode\s-\smore\serrors,\sat\sleast\sby\r\npeople\sthat\sare\snot\svery\sfamiliar\swith\sTcl-API,\sso\syou'll\sreally\sforce\sthem\salways\sto\swrite\sall\sthis\saround\sit?).\r\n\r\nThe\squestion\sis\sWHY\sit\sshould\sbe?!\sSo\swhy\sit\sall\sshould\sbe\snecessary\sjust\sto\sassign\san\sobject\sto\sthe\svariable?\r\n\r\nThis\s(in\smy\sopinion\scontroversial)\sbehavior\scan\sbe\seasy\s"fixed":\r\n\r\n-\seither\susing\sspecial\sflag\s(like\ssuggested\sbelow)\s-\sTCL_OWN_OBJREF,\swhich\swill\salways\sdecrease\sthe\srefCount\sin\serror\r\n\s\scase\s(so\sthe\scaller\scan\scontrol\sit\sresp.\sdoes\sthe\sdecision\swhat\sshould\shappen\swith\shim);\r\n-\sor\susing\spre-checking\sof\srefCount\sinside\sset-functions\s(before\svariable-lookup\sresp.\sassignment\stakes\splace,\sand\r\n\s\sbefore\sthe\sobject\sgets\sno\smore\ssane\sstate\sin\sundefined\serror\scase).\r\n-\sor\seven\sboth\sto\sbe\stotally\sbackwards-compatible\sin\sall\scases\sand\sto\sprovide\sfor\sdevelopers\sthe\sopportunity\sto\sdecide,\r\n\s\swhat\sshould\shappen.\r\n\r\nMeanwhile\sI've\simplemented\sboth\sthings\sfor\smy\sown\stcl-fork,\sand\sit\sworks\sfine\sand\ssaves\sme\s\r\nmany\stime,\swork\sand\scode-lines\s(not\sto\smention\sthe\sreadability).
J login sebres
J mimetype text/plain
K 578155d5a19b348dc1a9fe96cc2c067a59326a89
U sebres
Z e30242c49d1eabd4282f550e484d6ec0