Tcl Source Code

View Ticket
Login
2026-10-19
18:00 • Ticket [96c3f3b47d] No processing of after events status still Open with 4 o... artifact: 34bc53d0f9 user: serhiy.storchaka
17:55
Fix [96c3f3b47d] and [1802895fff]: [after] with a delay over about 12 years overflowed the seconds w... leaf check-in: d7e3050ca6 user: serhiy.storchaka tags: after-overflow-8-6
2025-05-06
09:34 • Ticket [96c3f3b47d] No processing of after events status still Open with 3 o... artifact: 9d736bb10f user: sbron
08:21 • Ticket [96c3f3b47d]: 4 changes artifact: db408fad1f user: oehhar
2017-02-16
00:41 • Ticket [96c3f3b47d]: 3 changes artifact: 18721a69ad user: chw
2017-02-15
20:49 • Ticket [96c3f3b47d]: 3 changes artifact: 9529a5e2da user: sbron
2017-02-13
22:28 • Ticket [96c3f3b47d]: 3 changes artifact: 7cd604dd7d user: aspect
18:18 • Ticket [96c3f3b47d]: 3 changes artifact: 635141634d user: dgp
13:11 • Ticket [96c3f3b47d]: 3 changes artifact: dffda43b62 user: aspect
12:24
Tcl_GetWideIntFromObj must fail for values between WIDE_MAX+1 and UWIDE_MAX (bug [96c3f3b47d1]) leaf check-in: 62e40d8270 user: aspect tags: bug-96c3f3b47d1
2017-02-12
22:49 • Ticket [96c3f3b47d] No processing of after events status still Open with 3 o... artifact: 487c2d7449 user: aspect
14:41 • New ticket [96c3f3b47d]. artifact: 7f617700ad user: sbron

Ticket UUID: 96c3f3b47d13052efb84fefb9328a195f407d859
Title: No processing of after events
Type: Bug Created on: 2017-02-12 14:41:02
Submitter: sbron Assigned to: nobody
Subsystem: 03. Timer Events Severity: Minor
Priority: 5 Medium Last modified: 2026-10-19 18:00:22
Status: Open Closed by: nobody
Resolution: None Closed on:
Version: 8.6.18
Description:
after 665544332211 {puts Byebye}
after 0 {puts Hello?}

On a 32-bit system, the first after event ends up in the linked list of timer events with some negative number for its sec value. As a result the second after event is placed behind the first one in the linked list. The calculation for how long to block in TimerSetupProc() does produce the correct (positive) number for the first after event. So presumably that will fire in 20+ years. As the first event in the list has to fire before the second one will, the [puts Hello?] never happened (within the time I was willing to wait).

This problem also causes a side effect with file event handlers. If a handler doesn't read all data, the readable event is also rescheduled with a timer, which again won't fire for more than 20 years.

From experience I can testify that it's not at all straightforward to trace problems with reading data from sockets back to a bad value passed to an after event in a different interpreter.

So either an error should be thrown when the ms value passed to after is too large, or the sorting of timers in the link list must be improved.
User Comments:
aspect added on 2017-02-12 22:49:49:
nice find!  This will probably fix neat(ish)ly with [0520d17284].

On a 64-bit build I see something related:

% after 9223372036854775808 {puts one}  ;# 1<<63
after#0
% after 0 {puts two}
after#1
% vwait forever
one
two

Damn c-types .. stdint types would make this easier to fix.  But that would break ABI compatibility since Tcl_Time itself is public.

It's not entirely clear if timeouts so far in the future that they exceed the capacity of Tcl_Time should simply raise an error, or be re-scheduled every TCL_TIME_MAX seconds by a helper proc.

Regarding file event handlers, is there a circumstance under which they will tickle this bug (by overflowing the after time), rather than just being affected by it?  I see ChannelTimerProc uses SYNTHETIC_EVENT_TIME (== 0), but it file events can calculate a delay this large that sounds like another bug.

aspect added on 2017-02-13 13:11:44:
So the underlying problem here is that Tcl_GetWideIntFromObj() produces a negative result for bignum values between WIDE_MAX+1 and UWIDE_MAX.  The manual seems pretty clear that it shouldn't, and I'm certain it breaks the expectations of callers elsewhere.  Straightforward to fix in itself, but nothing is simple ...

Tcl_GetLongFromObj and even Tcl_GetIntFromObj are also affected:

% testgetint [expr {1<<31}]
-2147483648

GLFO and GWIFO are not exposed to the test suite; they will need to be to shepherd this change in.

mathfunc.n clearly states that wide() and int() truncate bitwise .. and at least ExprWideFunc relies on the cooperation of Tcl_GetWideIntFromObj's cooperation to do so:

% expr {0 > int(1 << ($tcl_platform(wordSize)*8 - 1))}
1


I think mathfunc's contract is well-defined in the presence of negative bignums, but thinking about it is starting to hurt my head.

dgp added on 2017-02-13 18:18:10:
% expr {wide(0xffffffffffffffff) < 0}
1

This is the way it's supposed to work.

aspect added on 2017-02-13 22:28:30:
Clearly true, but is the same expected of Tcl_GetWideIntFromObj?

sbron added on 2017-02-15 20:49:44:
Unfortunately I think a lot of data presented here has no relationship with the problem. First of all 665544332211 is fine coming out of Tcl_GetWideIntFromObj(). Also your 64-bit example is mostly harmless. Sure, the {puts one} event fires too early, but it doesn't block other events from running at their proper times. That is the main problem I was trying to raise.

What happens with 665544332211 on a 32-bit system is that the specified number of milliseconds is divided by 1000 and added to the current time in seconds. This then gets stored in a 32-bit integer. While sorting the linked list of timer events (TclCreateAbsoluteTimerHandler()), this number is treated as negative and thus ends up at the head of the list. However, when calculating the timeout for the first event in the linked list (TimerSetupProc()), the current time is subtracted, making the value a large positive number again. This blocks other events in the linked list from running for a long time.

To create a similar problem on a 64-bit machine, you would have to end up with a number exceeding 1<<63 after dividing by 1000 and adding the current time. For the foreseeable future that would actually require at least a 73-bit number. Trying to use such a big number causes after to throw an error. So no big problem there.

Maybe all negative values passed to after should be converted to 0 and TclCreateAbsoluteTimerHandler() should treat time->sec as an unsigned integer while sorting the linked list. Doing that will only leave a (much less severe) problem on 32-bit systems with events scheduled after 2106. They would fire at the wrong time, but won't affect other events. Catching that problem too could be done by checking that wakeup.sec + (long)(ms / 1000) actually fits in an unsigned integer.

chw added on 2017-02-16 00:41:37:
Maybe the attached patch against tclTimer.c in core-8-6-branch fixes this.

It clamps the maximum delay to 0x7fffffff milliseconds for Tcl_CreateTimerHandler() and to about LONG_MAX seconds for
the after command.

Test case on a 32 bit machine:

after 0x600 {puts hoo}
after [expr {0x80000000 * 1000}] {puts boo}
after 0x800 {puts woah}

--- tclTimer.c.orig     2016-09-02 07:02:59.968037508 +0200
+++ tclTimer.c  2017-02-16 01:25:06.847730219 +0100
@@ -121,7 +121,8 @@
  */
 
 #define TCL_TIME_BEFORE(t1, t2) \
-    (((t1).sec<(t2).sec) || ((t1).sec==(t2).sec && (t1).usec<(t2).usec))
+    ((((t1).sec - (t2).sec) < 0) || \
+           ((t1).sec == (t2).sec && ((t1).usec - (t2).usec) < 0))
 
 #define TCL_TIME_DIFF_MS(t1, t2) \
     (1000*((Tcl_WideInt)(t1).sec - (Tcl_WideInt)(t2).sec) + \
@@ -259,6 +260,9 @@
      * Compute when the event should fire.
      */
 
+    if (milliseconds < 0) {
+       milliseconds = 0;
+    }
     Tcl_GetTime(&time);
     time.sec += milliseconds/1000;
     time.usec += (milliseconds%1000)*1000;
@@ -873,11 +877,16 @@
        afterPtr->id = tsdPtr->afterId;
        tsdPtr->afterId += 1;
        Tcl_GetTime(&wakeup);
-       wakeup.sec += (long)(ms / 1000);
-       wakeup.usec += ((long)(ms % 1000)) * 1000;
-       if (wakeup.usec > 1000000) {
-           wakeup.sec++;
-           wakeup.usec -= 1000000;
+       if (((wakeup.sec + (long)(ms / 1000)) - (wakeup.sec)) < 0) {
+           wakeup.sec += LONG_MAX - 1;
+           /* Don't consider fractional part. */
+       } else {
+           wakeup.sec += (long)(ms / 1000);
+           wakeup.usec += ((long)(ms % 1000)) * 1000;
+           if (wakeup.usec > 1000000) {
+               wakeup.sec++;
+               wakeup.usec -= 1000000;
+           }
        }
        afterPtr->token = TclCreateAbsoluteTimerHandler(&wakeup,
                AfterProc, afterPtr);

oehhar added on 2025-05-06 08:21:59:

Schelte, do you have any comments on the given patch?

For me, that looks right.

THanks, Harald


sbron added on 2025-05-06 09:34:47:

The patch looks good to me too. However, 8 years after raising the issue I no longer have a 32-bit system available to test.


serhiy.storchaka added on 2026-10-19 18:00:22:

Proposed fix for 8.6 in branch after-overflow-8-6, in the spirit of chw's patch: both forms of [after] add the delay with a new TimeAddMs(), which clamps the wakeup time (to 2038-01-19 where long is 32 bits) instead of letting it wrap. Tested with a MinGW Win64 build (32-bit long) under Wine: with after [expr {2**40}] ... pending, an after 200 never fired before and fires after 201 ms now, and the blocking form no longer returns at once. Tests timer-11.3 and 11.4. Tcl 9 is not affected: Tcl_Time.sec is 64 bits there.