Skip to content
Tcl Source Code Artifact [6cff7f725f]
Login

Artifact [6cff7f725f]

Artifact 6cff7f725f11bd4ffa1e75e41b028f79f6d37728abf08d22f0200eb0aa2cb80d:

Ticket change [6cff7f725f] - New ticket [97dbd7a4e9290821|97dbd7a4e9] <i>lseq: out of range double count is converted to Tcl_WideInt (undefined behavior)</i>. by anonymous on 2026-11-24 08:38:35.
D 2026-11-24T08:38:35.278
J assignee nobody
J closer nobody
J cmimetype text/x-fossil-plain
J comment GenerateArithSeries()\sin\sgeneric/tclExecute.c\scasts\sa\sdouble\scount\sto\r\nTcl_WideInt\sand\sthen\schecks\swhether\sthe\sconversion\swas\sexact:\r\n\r\n\s\s\s\sdouble\sdCount\s=\s*((const\sdouble\s*)\sptr);\r\n\s\s\s\sTcl_WideInt\swCount\s=\s(Tcl_WideInt)\sdCount;\r\n\s\s\s\sif\s(dCount\s-\swCount\s==\s0.0)\s{\r\n\r\nFor\sInf\sor\sa\svalue\soutside\sthe\sTcl_WideInt\srange\sthe\scast\sis\sundefined\r\nbehavior\sin\sC,\sso\sthe\scompiler\smay\sassume\sit\sdoesn't\shappen.\sclang\s23\swith\r\n-O2\s-march=x86-64-v3\sfolds\sthe\scheck\sinto\sa\splain\s"is\sintegral"\stest;\sthe\r\ncount\sthen\sbecomes\sWIDE_MIN\sand\s"lseq\s1e5555"\sreturns\san\sempty\slist\r\ninstead\sof\sraising\san\serror,\sso\stest\slseq-4.21.1\sfails.\r\n\r\nTo\sreproduce\s(unix,\strunk):\r\n\r\n\s\s\s\s./configure\sCC=clang\sCFLAGS="-O2\s-march=x86-64-v3"\s&&\smake\stclsh\r\n\s\s\s\secho\s'puts\s[catch\s{lseq\s1e5555}\sr];\sputs\s"<$r>"'\s|\s./tclsh\r\n\s\s\s\s->\s0\r\n\s\s\s\s\s\s\s<>\r\n\s\s\s\s(expected:\s1\s/\s<expected\sinteger\sbut\sgot\s"1e5555">)\r\n\r\nWith\splain\s-O2\sthe\sproblem\sdoes\snot\sshow\sup;\swe\shit\sit\sin\sOpenEmbedded\r\nbuilds\sthat\suse\sclang.\r\n\r\nThe\sattached\spatch\schecks\sthe\srange\sbefore\sconverting,\sand\sotherwise\r\nleaves\sthe\scount\sas\sa\sdouble\sso\sTclNewArithSeriesObj()\sreports\sthe\serror.\r\nWith\sit,\sthe\sreproducer\sgives\sthe\sexpected\serror\sand\slseq.test\spasses\r\n(Total\s145\sPassed\s130\sSkipped\s15\sFailed\s0).\r\n\r\nPatch\sfix\r\n\r\ndiff\s--git\sa/generic/tclExecute.c\sb/generic/tclExecute.c\r\nindex\s275529d..cc6418a\s100644\r\n---\sa/generic/tclExecute.c\r\n+++\sb/generic/tclExecute.c\r\n@@\s-9452,7\s+9452,17\s@@\sGenerateArithSeries(\r\n\s\s\s\s\s\s\s\sswitch\s(type)\s{\r\n\s\s\s\s\s\s\s\scase\sTCL_NUMBER_DOUBLE:\s{\r\n\s\s\s\s\s\s\s\s\s\s\s\sdouble\sdCount\s=\s*((const\sdouble\s*)\sptr);\r\n-\s\s\s\s\s\s\s\s\s\s\sTcl_WideInt\swCount\s=\s(Tcl_WideInt)\sdCount;\r\n+\s\s\s\s\s\s\s\s\s\s\sTcl_WideInt\swCount;\r\n+\r\n+\s\s\s\s\s\s\s\s\s\s\s/*\r\n+\s\s\s\s\s\s\s\s\s\s\s\s*\sCasting\san\sout\sof\srange\sdouble\s(including\sInf)\sto\san\sinteger\r\n+\s\s\s\s\s\s\s\s\s\s\s\s*\sis\sundefined\sbehavior,\sand\scompilers\smay\sfold\sthe\scheck\sbelow\r\n+\s\s\s\s\s\s\s\s\s\s\s\s*\sinto\sa\splain\s"is\sintegral"\stest,\sso\sdo\sthe\srange\scheck\sfirst.\r\n+\s\s\s\s\s\s\s\s\s\s\s\s*/\r\n+\s\s\s\s\s\s\s\s\s\s\sif\s(!(dCount\s>\s(double)\sWIDE_MIN\s&&\sdCount\s<\s(double)\sWIDE_MAX))\s{\r\n+\s\s\s\s\s\s\s\s\s\s\s\s\s\s\sbreak;\r\n+\s\s\s\s\s\s\s\s\s\s\s}\r\n+\s\s\s\s\s\s\s\s\s\s\swCount\s=\s(Tcl_WideInt)\sdCount;\r\n\s\s\s\s\s\s\s\s\s\s\s\sif\s(dCount\s-\swCount\s==\s0.0)\s{\r\n\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\sTcl_DecrRefCount(count);\r\n\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s//\sSwitch\sto\sthe\sobject\sholding\sinteger\sversion\sof\sthe\scount.
J foundin trunk\s(also\s9.1.0)
J is_private 0
J login anonymous
J priority 5\sMedium
J resolution None
J severity Minor
J status Open
J submitter anonymous
J subsystem None
J title lseq:\sout\sof\srange\sdouble\scount\sis\sconverted\sto\sTcl_WideInt\s(undefined\sbehavior)
J type Bug
K 97dbd7a4e9290821f20233c144077801424433e4
U anonymous
Z 1a9258326fc8432f63484be2d1d37e13