Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 989d74d3c038e84e82cdea2bfd8f470e77fd170e13dc15db69058030b4c689ff
Ticket: 00655c867eabd57ac3acd93bffe2f93799c12c89
ClockGetdatefieldsObjCmd(): avoid signed integer overflow and platform-dependent behavior
User & Date: chrstphrchvz on 2023-09-20 22:02:46
Changes

  1. comment changed to:
    clock-61.3 triggers an <tt>-fsanitize=signed-integer-overflow</tt> error:
    <pre>
    generic/tclClock.c:472:52: runtime error: signed integer overflow: 9223372036854775807 + 210866803200 cannot be represented in type 'long long'
    </pre>
    
    See attached patch. To avoid the overflow, I suggest dividing by <tt>SECONDS_PER_DAY</tt> first and then adding <tt>JULIAN_DAY_POSIX_EPOCH</tt> instead. The quotient also needs to be consistently rounded down, rather truncated towards 0 (GNU C and C99) or however a particular platform chooses (C89); otherwise incorrect results are produced for time values prior to the POSIX epoch. Not rounding down already produces incorrect results for time values prior to Julian day 0; the proposed clock-7.10 test currently fails with 
    <pre><span style="color:firebrick">-210866803200</span> -210866889600 -1 -210866803201 <span style="color:firebrick">0</span> -210866803200 0 <span style="color:firebrick">0</span> <span style="color:firebrick">0</span></pre>
     when integer division truncates toward 0.
    
  2. icomment:
    I neglected how <tt>clock add … days</tt> takes <tt>-timezone</tt> into consideration. Specifying <tt>-timezone :UTC</tt> to <tt>clock add … days</tt> avoids the failure on <tt>OPTS=static,msvcrt</tt>, although whether <tt>GetSystemTimeZone</tt> is correct or unexpectedly <tt>:localtime</tt> is beyond the scope of this test. <tt>-timezone</tt> should likely also be specified in the test’s other <tt>clock add</tt> usage for consistency. See attached patch.
    
  3. login: "chrstphrchvz"
  4. mimetype: "text/x-fossil-wiki"