Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: d29e03a74692ffa9b896a7eda2f474c0c9af4659c22ec0469a48cc83c25b7428
Ticket: 9c7557160fd1fb402aa1eb3ec4b358cfa79d596f
signed integer overflow in UpdateStringOfByteArray()
User & Date: chrstphrchvz on 2022-02-18 20:07:21
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/x-fossil-wiki"
  4. comment changed to:
    <tt>int size</tt> overflowing when incremented is undefined behavior. Example script and output showing UBSan (<tt>-fsanitize=signed-integer-overflow</tt>) error:
    <blockquote><pre>
    set f &#91;open /dev/zero &#91;list RDONLY BINARY&#93;&#93;
    fconfigure $f -buffersize &#91;expr {2**20}&#93;
    set z &#91;read $f &#91;expr {2**30}&#93;&#93;
    puts &#91;::tcl::mathop::ni $z &#91;list&#93;&#93;
    </pre></blockquote>
    <blockquote><pre>
    <span style="font-weight:bold">tcl/generic/tclBinary.c:568:10:<span style="color:firebrick"> runtime error: </span>signed integer overflow: 2147483647 + 1 cannot be represented in type 'int'</span>
    max size for a Tcl value (2147483647 bytes) exceeded
    </pre>(tclsh aborted)</blockquote>
    
    If <tt>size</tt> does not overflow, it could instead be <tt>INT_MAX</tt>, causing overflow in <tt>ckalloc(size + 1)</tt>. Example script and output:
    
    <blockquote><pre>
    set f &#91;open /dev/zero &#91;list RDONLY BINARY&#93;&#93;
    fconfigure $f -buffersize &#91;expr {2**20}&#93;
    set a &#91;encoding convertto utf-8 a&#93;
    set z &#91;read $f &#91;expr {2**30 - 1}&#93;&#93;
    set az &#91;string cat $a $z&#93;
    puts &#91;::tcl::mathop::ni $az &#91;list&#93;&#93;
    </pre></blockquote>
    
    <blockquote><pre>
    <span style="font-weight:bold">tcl/generic/tclBinary.c:575:19:<span style="color:firebrick"> runtime error: </span>signed integer overflow: 2147483647 + 1 cannot be represented in type 'int'</span>
    1
    </pre></blockquote>
    
    Casting <tt>size</tt> to <tt>unsigned int</tt> avoids this; see attached patch. (There could be alternatives such as declaring <tt>size<tt> as <tt>unsigned int</tt>.)
    
  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: "12. ByteArray Object"
  14. title changed to:
    signed integer overflow in UpdateStringOfByteArray()
    
  15. type changed to: "Patch"