Skip to content
Tcl Source Code Artifact [d90e46e1cb]
Login

Artifact [d90e46e1cb]

Artifact d90e46e1cbe28b9e32a5c91f394e204585e9441e2a8e3f9a7c8be2a0f28a202b:

Ticket change [d90e46e1cb] - New ticket [91c9bc1c457fda26|91c9bc1c45] <i>Standard channels (stdin, stdout, stderr) on Windows may get broken by dup2()</i>. by abv on 2018-11-09 21:51:41.
D 2018-11-09T21:51:41.611
J assignee nobody
J closer nobody
J cmimetype text/x-fossil-wiki
J comment 1.\sThe\sproblem\s(Tcl\slevel,\sroot\scause)\r\n\r\nOn\sWindows,\sstandard\schannels\s(stdin,\sstdout,\sstderr)\sin\sTcl\sare\sinitialized\sby\sOS\shandles\sreturned\sby\sWinAPI\sfunction\sGetStdHandle()\s(see\sTclpGetDefaultStdChannel()\sdefined\sin\swin/tclWinChan.c).\r\n\r\nThese\shandles\smay\sbe\sinvalidated\s(closed,\sreopened,\sreassigned\sto\sdifferent\skind\sof\sobject)\sby\sC/C++\scode.\sIn\sparticular,\sfunction\s_dup2()\sof\sstandard\sC\slibrary\s(MSVC),\swhen\scalled\sfor\sthe\sstandard\sfile\snumber\s(0,\s1,\s2)\sas\ssecond\sargument,\scloses\sthe\sOS\shandle\sassociated\swith\sthe\sstandard\sstream,\sthen\screates\sthe\snew\shandle\sand\ssets\sis\sas\sstandard\sone\sby\scall\sto\sSetStdHandle().\s\r\n\r\nIn\smost\scases\sthe\sold\sand\snew\shandles\sare\sthe\ssame\s(apparently\sdue\sto\sreuse),\sthus\sthere\sare\sno\simmediate\sconsequences.\sHowever,\ssometimes\s(in\smy\sexperiments\sabout\sonce\sper\sseveral\sthousand\scalls),\sthe\snew\sstandard\shandle\sassigned\sby\sthe\ssystem\sis\sdifferent\sfrom\sthe\sold\sone.\sYet\sTcl\schannel\sstill\skeeps\sthe\sold\shandle\sand\swhen\strying\sto\suse\sthat\schannel\s(e.g.\suse\sputs\sto\swrite\sto\sstdout),\serror\soccurs.\r\n\r\n2.\sThe\sproblem\s(application\slevel,\sreproducer)\r\n\r\nWe\sare\susing\sTcl\sas\scommand-line\stool\sto\sorganize\stesting\sof\sa\sC++\slibrary.\r\nWhen\sthe\stest\sis\sexecuted\sinteractively,\sthe\stest\ssystem\sintercepts\sthe\soutput\sto\sstdout\sand\sstderr\sstreams\s(from\sC\scode)\sso\sthat\sit\scan\sbe\sanalysed.\r\n\r\nIn\sthis\scontext,\ssometimes\sexecution\sof\sa\stest\sscript\sends\sup\swith\sTcl\sreporting\s\r\n"error\swriting\s"stdout":\sbad\sfile\snumber"\r\n\r\nAlas\sI\shave\sno\sisolated\sreproducer\sfor\sthe\sproblem\s(no\ssufficient\sexpertise\swith\sTcl).\r\nHowever\sit\scan\sbe\sreproduced\swithin\sa\stest\ssystem\s(the\ssoftware\sis\sopen\ssource),\sas\sfollows:\r\n\r\na)\sInstall\sOCCT\seither\sfrom\shttps://www.opencascade.com/content/latest-release\sor\sbuild\sit\sfrom\ssources\s(download\slink:\shttp://git.dev.opencascade.org/gitweb/?p=occt.git;a=snapshot;h=refs/tags/V7_3_0;sf=zip)\s\s\r\n\r\nb)\sRun\sdraw.bat\r\n\r\nc)\sType\s"test\sperf\sbop\sboxholes"\r\n\r\nThe\sproblem\shas\sbeen\sreproducible\sfor\syears,\swith\sTcl\s8.5\s-\s8.6.\r\nFor\sdebugging\sthe\sproblem\sI\shave\sused\sMSVC\s2017\s(15.8.4)\sCommunity\sEdition,\sworking\son\sWindows\s10\s64-bit.\r\nThe\slast\sTcl\sversion\stried\swas\sGitHub\sTcl\srepository\smaster\sas\sof\sNov\s06\r\n\r\n3.\sThe\sproposed\ssolution\r\n\r\nThe\sproblem\scan\sbe\ssolved\sby\sduplicating\sthe\sstandard\shandle\sreturned\sby\sGetStdHandle()\s(in\sTclpGetDefaultStdChannel())\sand\susing\sthe\sduplicate\sfor\sinitialization\sof\sthe\sTcl\schannel.\r\n\r\nHere\sis\sa\sdiff:\r\n\r\n<pre>\r\nwin/tclWinChan.c\s|\s11\s+++++++++++\r\n\s1\sfile\schanged,\s11\sinsertions(+)\r\n\r\ndiff\s--git\sa/win/tclWinChan.c\sb/win/tclWinChan.c\r\nindex\s8c47be646..480b387f4\s100644\r\n---\sa/win/tclWinChan.c\r\n+++\sb/win/tclWinChan.c\r\n@@\s-1302,6\s+1302,17\s@@\sTclpGetDefaultStdChannel(\r\n\s\treturn\s(Tcl_Channel)\sNULL;\r\n\s\s\s\s\s}\r\n\s\r\n+\s\s\s\s/*\r\n+\s\s\s\s\s*\sMake\sduplicate\sof\sthe\sstandard\shandle\sas\sit\smay\sbe\saltered\r\n+\s\s\s\s\s*\s(closed,\sreopened\swith\sanother\stype\sof\sthe\sobject\setc.)\sby\r\n+\s\s\s\s\s*\sthe\ssystem\sor\sa\suser\scode\sat\sany\stime,\se.g.\sby\scall\sto\s_dup2()\r\n+\s\s\s\s\s*/\r\n+\s\s\s\sif\s(!\sDuplicateHandle\s(GetCurrentProcess(),\shandle,\s\r\n+\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\sGetCurrentProcess(),\s&handle,\r\n+\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s0,\sFALSE,\sDUPLICATE_SAME_ACCESS))\s{\r\n+\treturn\s(Tcl_Channel)\sNULL;\r\n+\s\s\s\s}\r\n+\r\n\s\s\s\s\schannel\s=\sTcl_MakeFileChannel(handle,\smode);\r\n\s\r\n\s\s\s\s\sif\s(channel\s==\sNULL)\s{\r\n</pre>\r\n\r\nThe\sonly\sdrawback\sthat\sI\sforesee\sis\sthat\swhen\sstandard\sstream\sis\sredirected\son\sa\sC\slibrary\slevel,\sTcl\swill\skeep\sworking\swith\sinitial\sstream.\sThough\sthis\sshould\sbe\squite\slogical\staken\sthat\sTcl\shas\schannels\sbound\sto\sWinAPI\sand\snot\sstandard\sC\slibrary,\swhich\sis\sapparently\sintentional.
J foundin 8.6.8
J is_private 0
J login abv
J priority 5\sMedium
J private_contact 10ba58e00d474c6b8c6003e8e4dbe18be020bb7d
J resolution None
J severity Important
J status Open
J submitter abv
J subsystem 25.\sChannel\sSystem
J title Standard\schannels\s(stdin,\sstdout,\sstderr)\son\sWindows\smay\sget\sbroken\sby\sdup2()
J type Bug
K 91c9bc1c457fda269ae18595944fc3c2b54d961d
U abv
Z d1bc657d9b453d4a3074d0cda6e76e78