Artifacts Associated With Ticket 97dbd7a4e9
Ticket change [6cff7f725f] (rid 262061) by anonymous on 2026-11-24 08:38:35:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/x-fossil-plain"
- 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. - foundin initialized to: "trunk (also 9.1.0)"
- is_private initialized to: "0"
- login: "anonymous"
- priority initialized to: "5 Medium"
- resolution initialized to: "None"
- severity initialized to: "Minor"
- status initialized to: "Open"
- submitter initialized to: "anonymous"
- subsystem initialized to: "None"
- title initialized to:
lseq: out of range double count is converted to Tcl_WideInt (undefined behavior)
- type initialized to: "Bug"
Ticket change [b7aa3cea67] (rid 262068) by jan.nijtmans on 2026-11-24 10:58:30:
- assignee changed to: "jan.nijtmans"
- closedate changed to: "2461368.9572957177"
- closer changed to: "jan.nijtmans"
- icomment:
Thanks for the report! Fixed now in [dd40951b7f|trunk]
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- resolution changed to: "Fixed"
- status changed to: "Closed"