Skip to content
Tcl Source Code History Of Ticket 96c3f3b47d1
Login

History Of Ticket 96c3f3b47d1

Artifacts Associated With Ticket 96c3f3b47d1

  1. Ticket change [7f617700ad] (rid 151894) by sbron on 2017-02-12 14:41:02:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/plain"
    4. comment initialized to:
      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.
      
    5. foundin initialized to: "8.6.6"
    6. is_private initialized to: "0"
    7. login: "sbron"
    8. priority initialized to: "5 Medium"
    9. private_contact initialized to: "8475f421649231f1e116ac602bea4537d2b027b4"
    10. resolution initialized to: "None"
    11. severity initialized to: "Minor"
    12. status initialized to: "Open"
    13. submitter initialized to: "sbron"
    14. subsystem initialized to: "03. Timer Events"
    15. title initialized to: "No processing of after events"
    16. type initialized to: "Bug"
  2. Ticket change [487c2d7449] (rid 151897) by aspect on 2017-02-12 22:49:49:

    1. icomment:
      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.
      
    2. login: "aspect"
    3. mimetype: "text/plain"
  3. Ticket change [dffda43b62] (rid 151898) by aspect on 2017-02-13 13:11:44:

    1. icomment:
      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.
      
    2. login: "aspect"
    3. mimetype: "text/plain"
  4. Ticket change [635141634d] (rid 151900) by dgp on 2017-02-13 18:18:10:

    1. icomment:
      % expr {wide(0xffffffffffffffff) < 0}
      1
      
      This is the way it's supposed to work.
      
    2. login: "dgp"
    3. mimetype: "text/plain"
  5. Ticket change [7cd604dd7d] (rid 151901) by aspect on 2017-02-13 22:28:30:

    1. icomment:
      Clearly true, but is the same expected of Tcl_GetWideIntFromObj?
      
    2. login: "aspect"
    3. mimetype: "text/plain"
  6. Ticket change [9529a5e2da] (rid 151925) by sbron on 2017-02-15 20:49:44:

    1. icomment:
      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.
      
    2. login: "sbron"
    3. mimetype: "text/plain"
  7. Ticket change [18721a69ad] (rid 151927) by chw on 2017-02-16 00:41:37:

    1. icomment:
      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);
      
    2. login: "chw"
    3. mimetype: "text/plain"
  8. Ticket change [db408fad1f] (rid 239851) by oehhar on 2025-05-06 08:21:59:

    1. icomment:
      Schelte,
      do you have any comments on the given patch?
      
      For me, that looks right.
      
      THanks,
      Harald
      
    2. login: "oehhar"
    3. mimetype: "text/x-fossil-wiki"
    4. username: "oehhar"
  9. Ticket change [9d736bb10f] (rid 239869) by sbron on 2025-05-06 09:34:47:

    1. icomment:
      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.
      
    2. login: "sbron"
    3. mimetype: "text/x-fossil-wiki"
  10. Ticket change [34bc53d0f9] (rid 259314) by serhiy.storchaka on 2026-10-19 18:00:22:

    1. foundin changed to: "8.6.18"
    2. icomment:
      Proposed fix for 8.6 in branch [/timeline?r=after-overflow-8-6|after-overflow-8-6], in the spirit of chw's patch: both forms of <nowiki>[after]</nowiki> add the delay with a new <tt>TimeAddMs()</tt>, which clamps the wakeup time (to 2038-01-19 where <tt>long</tt> is 32 bits) instead of letting it wrap. Tested with a MinGW Win64 build (32-bit <tt>long</tt>) under Wine: with <tt><nowiki>after [expr {2**40}] ...</nowiki></tt> pending, an <tt>after 200</tt> 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: <tt>Tcl_Time.sec</tt> is 64 bits there.
      
    3. login: "serhiy.storchaka"
    4. mimetype: "text/x-fossil-wiki"