Skip to content
Tcl Source Code History Of Ticket 97dbd7a4e9
Login

History Of Ticket 97dbd7a4e9

Artifacts Associated With Ticket 97dbd7a4e9

  1. Ticket change [6cff7f725f] (rid 262061) by anonymous on 2026-11-24 08:38:35:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/x-fossil-plain"
    4. comment initialized to:
      GenerateArithSeries() in generic/tclExecute.c casts a double count to
      Tcl_WideInt and then checks whether the conversion was exact:
      
          double dCount = *((const double *) ptr);
          Tcl_WideInt wCount = (Tcl_WideInt) dCount;
          if (dCount - wCount == 0.0) {
      
      For Inf or a value outside the Tcl_WideInt range the cast is undefined
      behavior in C, so the compiler may assume it doesn't happen. clang 23 with
      -O2 -march=x86-64-v3 folds the check into a plain "is integral" test; the
      count then becomes WIDE_MIN and "lseq 1e5555" returns an empty list
      instead of raising an error, so test lseq-4.21.1 fails.
      
      To reproduce (unix, trunk):
      
          ./configure CC=clang CFLAGS="-O2 -march=x86-64-v3" && make tclsh
          echo 'puts [catch {lseq 1e5555} r]; puts "<$r>"' | ./tclsh
          -> 0
             <>
          (expected: 1 / <expected integer but got "1e5555">)
      
      With plain -O2 the problem does not show up; we hit it in OpenEmbedded
      builds that use clang.
      
      The attached patch checks the range before converting, and otherwise
      leaves the count as a double so TclNewArithSeriesObj() reports the error.
      With it, the reproducer gives the expected error and lseq.test passes
      (Total 145 Passed 130 Skipped 15 Failed 0).
      
      Patch fix
      
      diff --git a/generic/tclExecute.c b/generic/tclExecute.c
      index 275529d..cc6418a 100644
      --- a/generic/tclExecute.c
      +++ b/generic/tclExecute.c
      @@ -9452,7 +9452,17 @@ GenerateArithSeries(
              switch (type) {
              case TCL_NUMBER_DOUBLE: {
                  double dCount = *((const double *) ptr);
      -           Tcl_WideInt wCount = (Tcl_WideInt) dCount;
      +           Tcl_WideInt wCount;
      +
      +           /*
      +            * Casting an out of range double (including Inf) to an integer
      +            * is undefined behavior, and compilers may fold the check below
      +            * into a plain "is integral" test, so do the range check first.
      +            */
      +           if (!(dCount > (double) WIDE_MIN && dCount < (double) WIDE_MAX)) {
      +               break;
      +           }
      +           wCount = (Tcl_WideInt) dCount;
                  if (dCount - wCount == 0.0) {
                      Tcl_DecrRefCount(count);
                      // Switch to the object holding integer version of the count.
      
    5. foundin initialized to: "trunk (also 9.1.0)"
    6. is_private initialized to: "0"
    7. login: "anonymous"
    8. priority initialized to: "5 Medium"
    9. resolution initialized to: "None"
    10. severity initialized to: "Minor"
    11. status initialized to: "Open"
    12. submitter initialized to: "anonymous"
    13. subsystem initialized to: "None"
    14. title initialized to:
      lseq: out of range double count is converted to Tcl_WideInt (undefined behavior)
      
    15. type initialized to: "Bug"
  2. Ticket change [b7aa3cea67] (rid 262068) by jan.nijtmans on 2026-11-24 10:58:30:

    1. assignee changed to: "jan.nijtmans"
    2. closedate changed to: "2461368.9572957177"
    3. closer changed to: "jan.nijtmans"
    4. icomment:
      Thanks for the report!
      
      Fixed now in [dd40951b7f|trunk]
      
    5. login: "jan.nijtmans"
    6. mimetype: "text/x-fossil-wiki"
    7. resolution changed to: "Fixed"
    8. status changed to: "Closed"