Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 6cff7f725f11bd4ffa1e75e41b028f79f6d37728abf08d22f0200eb0aa2cb80d
Ticket: 97dbd7a4e9290821f20233c144077801424433e4
lseq: out of range double count is converted to Tcl_WideInt (undefined behavior)
User & Date: anonymous on 2026-11-24 08:38:35
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/x-fossil-plain"
  4. comment changed 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 changed to: "trunk (also 9.1.0)"
  6. is_private changed to: "0"
  7. login: "anonymous"
  8. priority changed to: "5 Medium"
  9. resolution changed to: "None"
  10. severity changed to: "Minor"
  11. status changed to: "Open"
  12. submitter changed to: "anonymous"
  13. subsystem changed to: "None"
  14. title changed to:
    lseq: out of range double count is converted to Tcl_WideInt (undefined behavior)
    
  15. type changed to: "Bug"