Overview
| Artifact ID: | e72c867bd361f34e4a23fe898b78b7dcf464d3ed930c66966a752323215ad18a |
|---|---|
| Ticket: | 1f40aa83c552f597c7aa2e01c7a3a5a2a3fe7c1c
crash with CFLAGS=-ftrapv |
| User & Date: | jan.nijtmans on 2024-03-22 09:12:50 |
Changes
- assignee changed to: "sebres"
- closer changed to: "nobody"
- icomment:
Re-opening, because there really is a bug, discovered by the -ftrapv option. First: <pre> $ tclsh9.0 % clock scan 27670116110564327424 -format %s integer value too large to represent % clock scan 27670116110564327424 Aborted </pre> The first answer is correct: 27670116110564327424 in hex is 0x18000000000000000, which - indeed - is too large to represent in 64-bit. Now: <pre> $ tclsh9.0 % clock scan 27670116110564327423 -format %s 9223372036854775807 % clock scan 27670116110564327423 Aborted </pre> Now, the first command gives an - incorrect - answer: The hex value of 27670116110564327423 is 0x17fffffffffffffff, which is also too large to represent in 64-bit. The second crash - most likely - happened because my build was with CFLAGS=-ftrapv, and an overflow occurred somewhere else. Compare this with Tcl 8.6: <pre> $ tclsh8.6 % clock scan 27670116110564327423 -format %s integer value too large to represent % clock scan 27670116110564327423 89170590265200 </pre> Explanation: Let's parse the string "27670116110564327423" with function _str2wideInt(). After 19 iterations, the string is parsed almost completely, only the last digit is missing. "prev" has the value 2767011611056432742, which is 0x2666666666666666 in hex, everything fine. Then "val" is multiplied by 10 and the last digit is added. Result is 0x17fffffffffffffff, which overflows and thus is truncated to 0x7fffffffffffffff. This means val > prev, and the function returns TCL_OK, without detecting that an overflow happened. I agree with you that usage of overflow trapping is bad .... in production code. But it's useful for finding bugs otherwise difficult to detect, as proven in this example: Whenever an overflow occurs during multiplication and this overflow is not detected, it might lead to a wrong answer without any indication that it happened. That's why a -ftrapv build is included in our build automation. It should never be used in production builds. My recommendation would be to use the function strtoll(). It's not ideal, because you need to check the overflow situation using errno (or - mayby - just by using *endptr), but it's better than using this (buggy) _str2wideInt(). But ... I'll leave the solution to you. A "knownBug" testcase is [a601eda829827c4c|here], now committed to the tip-688 branch. We don't need to stop the Voting: I'm sure this bug can be fixed (if you don't do it, I will) Thanks!
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- resolution changed to: "None"
- severity changed to: "Severe"
- status changed to: "Open"