Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2026-10-13
12:25 • Closed ticket [49538c09c8]: TclUnixWaitForFile: infinite wait reads uninitialize... artifact: 19a391b05d user: jan.nijtmans
2026-10-12
11:53
Fix [49538c09c8]: TclUnixWaitForFile: infinite wait may return early check-in: 8d02f97bd0 user: jan.nijtmans tags: core-9-0-branch
2026-10-11
21:01
Fix [49538c09c8]: TclUnixWaitForFile: infinite wait may return early check-in: 614f5eb377 user: jan.nijtmans tags: trunk, main
2026-09-25
17:39 • Ticket [49538c09c8] TclUnixWaitForFile: infinite wait reads uninitialized "... artifact: 86b7b3f0ef user: serhiy.storchaka
17:38
Fix [49538c09c8]: TclUnixWaitForFile: infinite wait reads uninitialized "now" and may return early. ... closed check-in: 65772037dd user: serhiy.storchaka tags: unix-waitforfile-infinite-wait
17:27 • New ticket [49538c09c8] TclUnixWaitForFile: infinite wait reads uninitialized &q... artifact: 9125bbfa6e user: serhiy.storchaka

TclUnixWaitForFile: infinite wait reads uninitialized "now" and may return early

Closed / Fixed49538c09c8d7e93ab4375a41023c6c293f01cb5eCreated 68 days agoUpdated 50 days ago
Descriptionwiki2026-09-25 17:27:46
In TclUnixWaitForFile() (unix/tclUnixNotfy.c) now is only assigned if timeout > 0, but the loop is
    do {
	...
	if (timeout < 0) {
	    continue;
	}
	now = Tcl_GetDayTime();
    } while (abortTime > now);

continue in a do-while jumps to the condition, so with timeout < 0 (wait forever), whenever poll() returns without a requested event (e.g. -1 with EINTR on a signal, or POLLNVAL), abortTime > now is evaluated with abortTime == 0 and an uninitialized now. Depending on the garbage value the function either waits again or returns 0 ("not ready") although the wait was infinite. GCC reports it as "'now' may be used uninitialized" (seen with gcc 8.3 on DragonFly and gcc 10.5 on NetBSD), although at the first use at line 595, which is guarded by timeout > 0.

WaitForConnect() in tclUnixSock.c calls it with -1 in a loop, which hides the early return there, but other callers (it is in the internal stubs table) are affected. 9.0 has the same loop (with Tcl_Time); 8.6 uses while (1) and is not affected.

Referenced by3
  • 8d02f97bd0Fix [49538c09c8]: TclUnixWaitForFile: infinite wait may return early check-in2026-10-12 11:53:47
  • 614f5eb377Fix [49538c09c8]: TclUnixWaitForFile: infinite wait may return early check-in2026-10-11 21:01:53
  • 65772037ddFix [49538c09c8]: TclUnixWaitForFile: infinite wait reads uninitialized "now" and may return early. With a negative timeout, "continue" in the do-while loop evaluated "abortTime > now" with an uninitialized "now", so if poll() returned without a requested event (e.g. EINTR on a signal), the function could return 0 although it should wait forever. Use while (1) with an explicit timeout check, as in the macOS version. New test event-13.9. check-in2026-09-25 17:38:17
serhiy.storchakawiki2026-09-25 17:39:09

Proposed fix in [65772037dd] (branch unix-waitforfile-infinite-wait): use while (1) with an explicit timeout check, as in the macOS version. New test event-13.9 interrupts an infinite wait with testalarm; without the fix the wait returns early.

jan.nijtmanswiki2026-10-13 12:25:48

Fixed in [trunk] and core-9-0-branch.

Tcl 8.6 didn't have this bug.

Many thanks!

Log in (anonymous is fine) to add a comment.