Overview
| Artifact ID: | 0754c669e5848f2a7d30b338b6b1b5c595b70f9a2404cde7ab043c9510fd46a2 |
|---|---|
| Ticket: | 89de4989734eb7720a8e7308e848b46e2f72f972
signed integer overflow in TclParseNumber() |
| User & Date: | chrstphrchvz on 2022-02-15 11:12:53 |
Changes
- assignee changed to: "nobody"
- closer changed to: "nobody"
- cmimetype changed to: "text/x-fossil-wiki"
- comment changed to:
Signed integer overflow (undefined behavior) occurs in <tt>TclParseNumber()</tt> (tclStrToD.c) for inputs equal to <tt>LONG_MIN</tt> or <tt>LLONG_MIN</tt>. UBSan (<tt>-fsanitize=signed-integer-overflow</tt>) errors for 32-bit <tt>long</tt> and 64-bit <tt>long long</tt>: <blockquote><pre> % set a 0; incr a -0o1000000000000000000000 <span style="font-weight:bold">tcl/generic/tclStrToD.c:1315:9:<span style="color:red"> runtime error: </span>negation of -9223372036854775808 cannot be represented in type 'long long int'; cast to an unsigned type to negate this value to itself</span> -9223372036854775808 % set b 0; incr b -0o20000000000 <span style="font-weight:bold">tcl/generic/tclStrToD.c:1330:5:<span style="color:red"> runtime error: </span>negation of -2147483648 cannot be represented in type 'long int'; cast to an unsigned type to negate this value to itself</span> -2147483648 % set c 0; incr c -0x8000000000000000 <span style="font-weight:bold">tcl/generic/tclStrToD.c:1362:9:<span style="color:red"> runtime error: </span>negation of -9223372036854775808 cannot be represented in type 'long long int'; cast to an unsigned type to negate this value to itself</span> -9223372036854775808 % set d 0; incr d -0x80000000 <span style="font-weight:bold">tcl/generic/tclStrToD.c:1377:5:<span style="color:red"> runtime error: </span>negation of -2147483648 cannot be represented in type 'long int'; cast to an unsigned type to negate this value to itself</span> -2147483648 </pre></blockquote> Negating before casting avoids this. However it may also be preferable to negate by subtracting from 0 (binary minus) to avoid compiler warnings for unary minus with unsigned operand (e.g. [https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-2-c4146|MSVC <tt>/W2</tt> warning C4146]). See attached patch.
- foundin changed to: "core-8-6-branch"
- is_private changed to: "0"
- login: "chrstphrchvz"
- priority changed to: "5 Medium"
- resolution changed to: "None"
- severity changed to: "Minor"
- status changed to: "Open"
- submitter changed to: "chrstphrchvz"
- subsystem changed to: "11. Conversions from String"
- title changed to: "signed integer overflow in TclParseNumber()"
- type changed to: "Patch"