Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 2bba8435d878ffc8060c701b4a58611b079f140842db599c0263162639628a4e
Ticket: 7f8a3d981843fe53d6a6a6bd659f3a3440bd15e3
signed integer overflow in tclExecute.c
User & Date: chrstphrchvz on 2022-02-15 22:46:40
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/x-fossil-wiki"
  4. comment changed to:
    tclExecute.c tries to check for signed integer overflow after the fact using the <tt>Overflowing()</tt> macro, but does not avoid undefined behavior from occuring in the expressions which overflow; casting operands to unsigned types avoids this (see attached patch).
    
    Below are example triggers for every instance I found and corresponding 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 0x7fffffff; incr a 1
    <span style="font-weight:bold">tcl/generic/tclExecute.c:1901:7:<span style="color:red"> runtime error: </span>signed integer overflow: 2147483647 + 1 cannot be represented in type 'long int'</span>
    2147483648
    % proc e3932 {} {global x; set x 0x7fffffff; incr x 1}
    % e3932
    <span style="font-weight:bold">tcl/generic/tclExecute.c:3932:12:<span style="color:red"> runtime error: </span>signed integer overflow: 2147483647 + 1 cannot be represented in type 'long int'</span>
    % proc e3980_1952 {} {global x; set x 0x7fffffffffffffff; incr x 1}
    % e3980_1952
    <span style="font-weight:bold">tcl/generic/tclExecute.c:3980:11:<span style="color:red"> runtime error: </span>signed integer overflow: 9223372036854775807 + 1 cannot be represented in type 'long long int'</span>
    <span style="font-weight:bold">tcl/generic/tclExecute.c:1952:6:<span style="color:red"> runtime error: </span>signed integer overflow: 9223372036854775807 + 1 cannot be represented in type 'long long int'</span>
    </pre></blockquote>
    
    For 64-bit <tt>long</tt>:
    
    <blockquote><pre>
    % proc e6509 {} {return [expr {0x7fffffffffffffff+1}]}
    % e6509
    <span style="font-weight:bold">tcl/generic/tclExecute.c:6509:16:<span style="color:red"> runtime error: </span>signed integer overflow: 9223372036854775807 + 1 cannot be represented in type 'long'</span>
    <span style="font-weight:bold">tcl/generic/tclExecute.c:9149:16:<span style="color:red"> runtime error: </span>signed integer overflow: 9223372036854775807 + 1 cannot be represented in type 'long'</span>
    9223372036854775808
    % proc e6524 {} {return [expr {-0x7fffffffffffffff-2}]}
    % e6524
    <span style="font-weight:bold">tcl/generic/tclExecute.c:6524:16:<span style="color:red"> runtime error: </span>signed integer overflow: -9223372036854775807 - 2 cannot be represented in type 'long'</span>
    <span style="font-weight:bold">tcl/generic/tclExecute.c:9165:16:<span style="color:red"> runtime error: </span>signed integer overflow: -9223372036854775807 - 2 cannot be represented in type 'long'</span>
    -9223372036854775809
    </pre></blockquote>
    
    (There is more signed integer overflow in tclExecute.c which I have yet to report/suggest fixes for.)
    
  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: "48. Number Handling"
  14. title changed to: "signed integer overflow in tclExecute.c"
  15. type changed to: "Patch"