Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 0754c669e5848f2a7d30b338b6b1b5c595b70f9a2404cde7ab043c9510fd46a2
Ticket: 89de4989734eb7720a8e7308e848b46e2f72f972
signed integer overflow in TclParseNumber()
User & Date: chrstphrchvz on 2022-02-15 11:12:53
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/x-fossil-wiki"
  4. 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.
    
  5. foundin changed to: "core-8-6-branch"
  6. is_private changed to: "0"
  7. login: "chrstphrchvz"
  8. priority changed to: "5 Medium"
  9. resolution changed to: "None"
  10. severity changed to: "Minor"
  11. status changed to: "Open"
  12. submitter changed to: "chrstphrchvz"
  13. subsystem changed to: "11. Conversions from String"
  14. title changed to: "signed integer overflow in TclParseNumber()"
  15. type changed to: "Patch"