Skip to content
Tcl Source Code Artifact [43a05eb648]
Login

Artifact [43a05eb648]

Artifact 43a05eb648dba510582c55e358b0936f4d69b439:

Ticket change [43a05eb648] - New ticket [979640ffffffffff|979640ffff] <i>Buffer overruns mixing C and Tcl environment changes</i>. by stevebold on 2004-06-25 10:30:22.
D 2004-06-25T10:30:22
J assignee hobbs
J cmimetype text/plain
J comment I\shave\san\sapplication\swhich\sembeds\sTcl\sbut\swhich\shas\nother\scomponents\snot\susing\sTcl.\sThe\sapp\sruns\sTcl\nscripts\swhich\smodify\sthe\senvironment\sbut\sthe\snon-Tcl\ncomponents\salso\smodify\sthe\senvironment\susing\sdirect\ncalls\sto\sthe\sC\sfunction\sputenv().\n\nThe\soriginal\sproblem\sI\sencountered\swas\sthat\sthe\napplication\ssometimes\scrashed\son\sstartup\swhen\srun\son\nLinux\susing\sTcl\s8.4.3\sand\sthe\scrash\soccurred\swhen\sin\na\scall\sto\sTclSetEnv().\sDebugging\sthe\sstartup\ssequence\nusing\sPurify\son\sSolaris\sshowed\sArray\sBuffer\sWrite\n(ABW)\serrors\swhen\sTcl\smodifies\sthe\senvironment,\nthough\son\sSolaris\sthis\sdid\snot\scause\sa\scrash.\sI\shave\nsince\sreproduced\sthe\sproblem\susing\s8.5a1\sand\sreduced\nthe\sapplication\sto\sa\ssmall\stest\scase\sinvolving\sone\nTcl\sscript\sand\sone\sC\sfile,\swhich\sare\sgiven\sbelow:\n\n/*\sCrashEnv.c\s-\scompile\sas\sa\sshared\slibrary\sCrashEnv.so\s\n*/\n#include\s<tcl.h>\n#include\s<stdlib.h>\n\nstatic\sint\smyCmd(ClientData\sdummy,Tcl_Interp*\s\ninterp,int\sobjc,Tcl_Obj*\sCONST*\sobjv)\n{\n\s\s\sputenv("a=b");\n}\n\n\nint\sEnvcrash_Init(Tcl_Interp*\sinterp)\n{\n\s\s\sif\s(Tcl_InitStubs(interp,"8.4",0)\s==\sNULL)\s{\n\s\s\s\s\s\sreturn\sTCL_ERROR;\n\s\s\s}\n\n\s\s\sTcl_CreateObjCommand(interp,"myCmd",myCmd,0,0);\n\s\s\sreturn\sTCL_OK;\n}\n/*\sEnd\sCrashEnv.c\s*/\n\n#\sCrashEnv.tcl\s-\ssource\sin\sa\stclsh\sbuilt\susing\sPurify\sand\s\nsee\san\sABW\serror.\nset\senv(m)\s1\nload\s[pwd]/EnvCrash.so\nmyCmd\nset\senv(l)\s2;\s#\sABW\shere.\n#\send\sCrashEnv.tcl\n\n\nI\sbelieve\sthe\sroot\sof\sproblem\sis\sthis:\n\nWhen\sTclSetEnv()\sis\sfirst\sasked\sto\sadd\sa\svariable\sto\nthe\senvironment,\sit\sdetermines\sthat\sthe\slength\sof\sthe\ncurrent\senvironment\sis\s10.\sIt\sneeds\sto\sallocate\sa\snew\nenviron\sarray\ssized\sto\sat\sleast\s12,\sallowing\sspace\nfor\sthe\snew\sentry\sand\snull\sterminator.\n\nIt\sactually\sallocates\s15\selements.\sThis\sgives\sit\nspace\sfor\san\sextra\s3\svariables,\sreducing\sthe\nfrequence\sof\sreallocations.\n\nSubsequently\sthe\sapplication\smakes\sa\sdirect\scall\sto\nputenv()\sand\sit\sfinds\sit\sneeds\sto\sadd\sa\svariable\sto\nthe\senvironment.\sIt\ssees\sthat\sthere\sare\s11\svariables\nin\sthe\senvironment.\sWith\sa\snew\svariable\sand\nterminator\sthe\srequired\slength\sis\s13,\sso\sit\sallocates\nthis\ssize\sas\sthe\snew\senviron\sarray.\n\nThe\sscript\sthen\smodifies\sthe\senvironment\sagain,\ntriggering\sanother\scall\sto\sTclSetEnv().\sIt\ssees\sthere\nare\s12\svariables\sin\sthe\senvironment.\sWith\sthe\snew\nvariable\sand\snull\sterminator,\sa\slength\sof\s14\sis\nneeded.\sIt\sremembers\sthat\sit\shas\spreviously\sallocated\nspace\sfor\s15\sand\sso\sdoes\snot\sextend\sthe\sarray.\nUnfortunately,\sthis\sisn't\ssafe.\n\nIt\sis\spossible\sthat\ssome\simplementations\sof\sputenv()\nemploy\ssimilar\soptimisations,\sso\sthe\ssame\sproblem\nmight\spossibly\soccur\sthe\sother\sway\saround.\sI\sconclude\nthat\syou\scannot\sallow\smultiple\scomponents\sto\smodify\nenviron.\n\nI\scan\swork\saround\sthe\sproblem\sby\ssetting\s$(ENV_FLAGS)\nto\s-DUSE_PUTENV\sin\sthe\sMakefile.\sPossibly\sthis\sis\sa\nvalid\sfix\sfor\sthe\sproblem?\n\n\nPurify\soutput\sfrom\sabove\stest\susing\s8.5a1\n\n****\s\sPurify\s\ninstrumented\s/eeg/home/stevebo/tcl8.5a1/unix/tclsh\s\n(pid\s1476)\s\s****\nABW:\sArray\sbounds\swrite:\n\s\s*\sThis\sis\soccurring\swhile\sin:\n\s\s\sTclSetEnv\s\s\s\s\s\s[tclEnv.c:214]\n\s\s\sEnvTraceProc\s\s\s[tclEnv.c:561]\n\s\s\sTclCallVarTraces\s[tclTrace.c:2516]\n\s\s\sTclPtrSetVar\s\s\s[tclVar.c:1679]\n\s\s\sTcl_ObjSetVar2\s[tclVar.c:1515]\n\s\s\sTcl_SetObjCmd\s\s[tclVar.c:1286]\n\s\s\sTclEvalObjvInternal\s[tclBasic.c:3147]\n\s\s\sTcl_EvalEx\s\s\s\s\s[tclBasic.c:3642]\n\s\s\sTcl_FSEvalFileEx\s[tclIOUtil.c:1665]\n\s\s\sTcl_Main\s\s\s\s\s\s\s[tclMain.c:380]\n\s\s\smain\s\s\s\s\s\s\s\s\s\s\s[tclAppInit.c:90]\n\s\s\s_start\s\s\s\s\s\s\s\s\s[crt1.o]\n\s\s*\sWriting\s4\sbytes\sto\s0xaa604\sin\sthe\sheap.\n\s\s*\sAddress\s0xaa604\sis\s1\sbyte\spast\send\sof\sa\smalloc'd\s\nblock\sat\s0xaa4d8\sof\s300\sbytes.\n\s\s*\sThis\sblock\swas\sallocated\sfrom:\n\s\s\smalloc\s\s\s\s\s\s\s\s\s[rtlib.o]\n\s\s\sputenv\s\s\s\s\s\s\s\s\s[libc.so.1]\n\s\s\smyCmd\s\s\s\s\s\s\s\s\s\s[EnvCrash.c]\n\s\s\sTclEvalObjvInternal\s[tclBasic.c:3147]\n\s\s\sTcl_EvalEx\s\s\s\s\s[tclBasic.c:3642]\n\s\s\sTcl_FSEvalFileEx\s[tclIOUtil.c:1665]\n\s\s\sTcl_Main\s\s\s\s\s\s\s[tclMain.c:380]\n\s\s\smain\s\s\s\s\s\s\s\s\s\s\s[tclAppInit.c:90]\n\s\s\s_start\s\s\s\s\s\s\s\s\s[crt1.o]\n
J foundin obsolete:\s8.5a1
J priority 5\sMedium
J resolution None
J status Open
J submitter stevebold
J subsystem 08.\sEnvironment\sVariables
J title Buffer\soverruns\smixing\sC\sand\sTcl\senvironment\schanges
J type Bug
K 979640ffffffffffffffffffffffffffffffffff
U stevebold
Z 8d67eac1d47cb75de02cc734459785aa