<i>TclUnixWaitForFile: infinite wait reads uninitialized "now" and may return early</i>.
by
.
D 2026-09-25T17:27:46.724
J cmimetype text/x-fossil-wiki
J comment In\s<code>TclUnixWaitForFile()</code>\s(unix/tclUnixNotfy.c)\s<code>now</code>\sis\sonly\sassigned\sif\s<code>timeout\s>\s0</code>,\sbut\sthe\sloop\sis\n\n<pre>\n\s\s\s\sdo\s{\n\t...\n\tif\s(timeout\s<\s0)\s{\n\t\s\s\s\scontinue;\n\t}\n\tnow\s=\sTcl_GetDayTime();\n\s\s\s\s}\swhile\s(abortTime\s>\snow);\n</pre>\n\n<code>continue</code>\sin\sa\sdo-while\sjumps\sto\sthe\scondition,\sso\swith\s<code>timeout\s<\s0</code>\s(wait\sforever),\swhenever\s<code>poll()</code>\sreturns\swithout\sa\srequested\sevent\s(e.g.\s-1\swith\sEINTR\son\sa\ssignal,\sor\sPOLLNVAL),\s<code>abortTime\s>\snow</code>\sis\sevaluated\swith\s<code>abortTime\s==\s0</code>\sand\san\suninitialized\s<code>now</code>.\sDepending\son\sthe\sgarbage\svalue\sthe\sfunction\seither\swaits\sagain\sor\sreturns\s0\s("not\sready")\salthough\sthe\swait\swas\sinfinite.\sGCC\sreports\sit\sas\s"'now'\smay\sbe\sused\suninitialized"\s(seen\swith\sgcc\s8.3\son\sDragonFly\sand\sgcc\s10.5\son\sNetBSD),\salthough\sat\sthe\sfirst\suse\sat\sline\s595,\swhich\sis\sguarded\sby\s<code>timeout\s>\s0</code>.\n\n<code>WaitForConnect()</code>\sin\stclUnixSock.c\scalls\sit\swith\s-1\sin\sa\sloop,\swhich\shides\sthe\searly\sreturn\sthere,\sbut\sother\scallers\s(it\sis\sin\sthe\sinternal\sstubs\stable)\sare\saffected.\s9.0\shas\sthe\ssame\sloop\s(with\s<code>Tcl_Time</code>);\s8.6\suses\s<code>while\s(1)</code>\sand\sis\snot\saffected.
J foundin 9.0.4,\s9.1b1
J login serhiy.storchaka
J priority 5\sMedium
J resolution None
J severity Minor
J status Open
J submitter serhiy.storchaka
J subsystem 01.\sNotifier
J title TclUnixWaitForFile:\sinfinite\swait\sreads\suninitialized\s"now"\sand\smay\sreturn\searly
J type Bug
K 49538c09c8d7e93ab4375a41023c6c293f01cb5e
U serhiy.storchaka
Z eb2c5efc6d33f7110069b9c285dd8ffd