Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2026-11-24
10:58 • Closed ticket [97dbd7a4e9]: lseq: out of range double count is converted to Tcl_... artifact: b7aa3cea67 user: jan.nijtmans
10:51
Fix [97dbd7a4e9]: lseq: out of range double count is converted to Tcl_WideInt (undefined behavior) check-in: dd40951b7f user: jan.nijtmans tags: trunk, main
08:38 • New ticket [97dbd7a4e9] lseq: out of range double count is converted to Tcl_Wide... artifact: 6cff7f725f user: anonymous

lseq: out of range double count is converted to Tcl_WideInt (undefined behavior)

Closed / Fixed97dbd7a4e9290821f20233c144077801424433e4Created 9 days agoUpdated 8 days ago
Descriptionplain2026-11-24 08:38:35
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.
Referenced by1
  • dd40951b7fFix [97dbd7a4e9]: lseq: out of range double count is converted to Tcl_WideInt (undefined behavior) check-in2026-11-24 10:51:39
jan.nijtmanswiki2026-11-24 10:58:30

Thanks for the report!

Fixed now in [trunk]

Log in (anonymous is fine) to add a comment.