Artifacts Associated With Ticket 3d3124d01d
Ticket change [6c3522e59a] (rid 137165) by anonymous on 2014-03-06 15:27:31:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/plain"
- comment initialized to:
If tcl is compiled with the "-ftrapv" option (which adds checks for integer overflow) the string to integer conversion code in tclStrToD.c fails when parsing the most negative 64-bit integer (-9223372036854775808). This manifests itself in the clock command: % clock format [clock seconds] -format "%d-%b-%Y" Process 4807 stopped * thread #1: tid = 0x4e9b38, 0x000000010009ebcc libtcl8.5.dylib`TclParseNumber(interp=0x0000000000000000, objPtr=<unavailable>, expected=0x00000001000ca82e, bytes=<unavailable>, numBytes=-1, endPtrPtr=0x0000000000000000, flags=<unavailable>) + 5628 at tclStrToD.c:560, queue = 'com.apple.main-thread, stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) frame #0: 0x000000010009ebcc libtcl8.5.dylib`TclParseNumber(interp=0x0000000000000000, objPtr=<unavailable>, expected=0x00000001000ca82e, bytes=<unavailable>, numBytes=-1, endPtrPtr=0x0000000000000000, flags=<unavailable>) + 5628 at tclStrToD.c:560 557 } else if (flags & TCL_PARSE_OCTAL_ONLY) { 558 goto zeroo; 559 } else if (isdigit(UCHAR(c))) { -> 560 significandWide = c - '0'; 561 numSigDigs = 1; 562 state = DECIMAL; 563 break; because the clock command at some point gets to here in clock.tcl: 3596 set r {} 3597 set lastTime $MINWIDE 3598 foreach t $times c $codes { 3599 if { $t < $lastTime } { 3600 return -code error "$fileName has times out of order" 3601 } 3602 set lastTime $t The crash happens in line 3599 when $t is compared with $MINWIDE. This code doesn't look quite right to me as $t can never be less than $lastTime the first time through as $lastTime is the most negative number. Shouldn't lastTime be initialized with the first time in the list? Or given that lastTime is presumably seconds the arbitrary choice of $MINWIDE to seed it could be somewhat different? I'm not sure whether the bug is in tclStrToD (because it deliberately overflows and -ftrapv should never be used) but there surely does seem to be a bug in clock.tcl as well. For reference this also fails: % set last -9223372036854775808 -9223372036854775808 % incr last Illegal instruction: 4 This is on OS X Mavericks with clang but colleagues report a similar problem on linux with gcc. Apologies if this is a duplicate as I've had trouble finding how to search the ticket database. - foundin initialized to: "8.5.15"
- is_private initialized to: "0"
- login: "anonymous"
- priority initialized to: "5 Medium"
- private_contact initialized to: "7de6c5b3e942e3c0d4c64472ecfc87b370d5b7b8"
- resolution initialized to: "None"
- severity initialized to: "Important"
- status initialized to: "Open"
- submitter initialized to: "anonymous"
- subsystem initialized to: "11. Conversions From String"
- title initialized to: "tclStrToD can fail with -ftrapv compiler option"
- type initialized to: "Bug"
Ticket change [a6d4a2267e] (rid 137272) by jan.nijtmans on 2014-03-18 11:19:01:
- assignee changed to: "kbk"
- login: "jan.nijtmans"
- mimetype: "text/plain"
Ticket change [dfa276621b] (rid 148446) by dkf on 2016-08-23 13:47:37:
- login: "dkf"
- mimetype: "text/plain"
- subsystem changed to: "11. Conversions from String"
Ticket change [60e67bcf7b] (rid 217227) by chrstphrchvz on 2023-08-15 07:26:51:
- icomment:
I am not certain which operation overflowed here. <tt>c - '0'</tt> only overflows when <tt>c < -0x50</tt>. If <tt>isdigit(UCHAR(c))</tt> is true, then <tt>c</tt> is between <tt>0x30</tt> and <tt>0x39</tt>, so <tt>c - '0'</tt> cannot overflow. Something else seems wrong. I do not know if declaring <tt>c</tt> as <tt>unsigned char</tt> (since <tt>char</tt> is signed by default on Darwin amd64) to avoid the <tt>-ftrapv</tt> exception is a good idea or just hides the problem. But I have not spotted any relevant changes since this issue was reported which may have resolved it. So far I cannot reproduce this issue with either <tt>-ftrapv</tt> or <tt>-fsanitize=signed-integer-overflow</tt> when I run these examples.
- login: "chrstphrchvz"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [66491bb365] (rid 217228) by chrstphrchvz on 2023-08-15 07:41:07:
- icomment:
<blockquote>c - '0' only overflows when c < -0x50.</blockquote> Sorry, this is wrong. <tt>c - '0'</tt> cannot overflow at all, because <tt>'0'</tt> by itself is <tt>int</tt> which causes <tt>c</tt> to be promoted, and <tt>c</tt> regardless of signedness cannot be in the range <tt>[INT_MIN,INT_MIN+'0')</tt> which are the only values that would lead to overflow. The mystery remains.
- login: "chrstphrchvz"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [115af88394] (rid 218160) by jan.nijtmans on 2023-09-14 07:42:37:
- foundin changed to: "8.6"
- icomment:
Well, I compiled Tcl 8.6 with the -ftrapv flag, the result was: <pre> $ make test-tcl TESTFLAGS="-file clock.test" ... Tests began at Thu Sep 14 09:35:58 CEST 2023 clock.test Test file error: child killed: illegal instruction Tests ended at Thu Sep 14 09:36:19 CEST 2023 all.tcl: Total 0 Passed 0 Skipped 0 Failed 0 Sourced 1 Test Files. Test files exiting with errors: clock.test </pre> All other test-cases pass, and the examples in this ticket pass to. The test-case failing was: <pre> test clock-61.3 {near-miss overflow of a wide integer on output} { clock format 0x7fffffffffffffff -format %s -gmt true } [expr 0x7fffffffffffffff] </pre> So it looks like not everything is solved in current 8.6. It's just the maximum number, not the minimum one, still causing problems. For 8.5 this would be a Wont-Fix, but if Kevin (or anyone else) wants to dig into this .... it still can be fixed for 8.6 - login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
Ticket change [cdf7fc5b30] (rid 218161) by jan.nijtmans on 2023-09-14 07:43:08:
- login: "jan.nijtmans"
- mimetype: "text/plain"
- title changed to: "clock-61.3 fails with -ftrapv compiler option"
Ticket change [58ba84cd3d] (rid 218162) by chrstphrchvz on 2023-09-14 08:27:18:
- icomment:
Jan, I believe what you encountered in clock-61.3 is what I reported in [b9a41897a8]. There are likely other instances of signed integer overflow in the clock code which I have encountered but not reported. When I said I could not reproduce this issue, I meant the issue specifically at the location in <tt>TclParseNumber()</tt> described by the reporter. Sorry for the confusion.
- login: "chrstphrchvz"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [f6f569b704] (rid 218206) by jan.nijtmans on 2023-09-14 21:21:37:
- closedate changed to: "2460202.39001356"
- closer changed to: "jan.nijtmans"
- icomment:
The fix [ee677ecc84710dfd|here] indeed fixes all testcases when using -ftrapv. Thanks!
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- resolution changed to: "Fixed"
- status changed to: "Closed"
Ticket change [e6722e725b] (rid 218207) by chrstphrchvz on 2023-09-14 22:44:19:
- closedate changed to: "2460202.44745293"
- closer changed to: "chrstphrchvz"
- icomment:
Pardon me obsessing over this… I still feel that clock-61.3 likely does not belong in the title of this ticket, as the reporter’s issue involved parsing -9223372036854775808, and shown much more succinctly by their <tt>incr</tt> example than by the <tt>$MINWIDE</tt> → <tt>$lastTime</tt> example in clock.tcl. I did not spot any relevant changes to the C code mentioned by the reporter, so maybe it is better to close this as “works for me” rather than “fixed” or “outdated”. I have also encountered reports of false negatives in GCC <tt>-ftrapv</tt> (and reportedly not affecting <tt>-fsanitize=signed-integer-overflow</tt>).
- login: "chrstphrchvz"
- mimetype: "text/x-fossil-wiki"
Ticket change [7da33fc311] (rid 218223) by jan.nijtmans on 2023-09-15 06:21:07:
- closer changed to: "nobody"
- icomment:
@chrstphrchvz, Many Thanks! As usual, you are right! Let's reserve this ticket for the -ftrapv option then, and let's make sure it continues working: [fbfde51043fdf463] I think it's OK to generalize tickets: Even though the original report was very specific (mentioning tclStrToD.c), it can be used to fix similar related issues which fall outside the original ticket specification but belong to the same idea.
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- status changed to: "Pending"
- title changed to: "failures with -ftrapv compiler option"
Ticket change [9e52e87dfc] (rid 218244) by jan.nijtmans on 2023-09-15 10:34:24:
- icomment:
Found 3 more places where -ftrapv errors are triggered, running the standard testsuite. See: [237894f65ad976db]. Easiest fix is just cast to (unsigned)
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [5fd1082054] (rid 218245) by chrstphrchvz on 2023-09-15 11:39:49:
- icomment:
I agree that building with <tt>-ftrapv</tt> as the reporter did would have encountered various other instances of signed integer overflow. However, there remains possibility of that throughout Tcl/Tk codebases, and often beyond existing test suite coverage. So I expect this to remain an ongoing and gradually uncovered issue, rather than something a single ticket can be designated for and addressed in a reasonable time period. I would suggest having a CI build with UBSan (<tt>-fsanitize=undefined</tt>) enabled. That is what I have encountered other major software projects doing, and will likely be much more useful than <tt>-ftrapv</tt>.
- login: "chrstphrchvz"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [f51b97469f] (rid 218246) by chrstphrchvz on 2023-09-15 13:23:24:
- icomment:
Regarding the overflow in tclExecute.c, I suggest looking at [ba731752d345] in case that warrants taking a different approach.
- login: "chrstphrchvz"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [473a0e4b55] (rid 218247) by chrstphrchvz on 2023-09-15 13:24:24:
- icomment:
(Disregard previous comment.) Regarding the overflow in tclExecute.c, I suggest looking at [176de86b3b] in case that warrants taking a different approach.
- login: "chrstphrchvz"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [bd9254701a] (rid 258142) by serhiy.storchaka on 2026-10-15 11:29:07:
- icomment: "Closing: fixed, see above."
- login: "serhiy.storchaka"
- mimetype: "text/x-fossil-wiki"
- status changed to: "Closed"