Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 5e4cad2b3e2558cea9d678fc93b0bc4060a82a860b1a4819d8ee3717f42ebd48
Ticket: 69b4f7eb379e7b806c0c144a36059ca55f569634
FilterInputBytes(): signed integer overflow, poor worst-case performance lengthening objPtr
User & Date: chrstphrchvz on 2022-03-06 20:28:47
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/x-fossil-wiki"
  4. comment changed to:
    Running this example leads to signed integer overflow in <tt>FilterInputBytes()</tt> (as reported by UBSan <tt>-fsanitize=signed-integer-overflow</tt>):
    
    <pre>
    $ dd bs=4096 count=250000 if=/dev/zero | tclsh8.6
    <span style="font-weight:bold">tcl/generic/tclIO.c:5330:22:<span style="color:firebrick"> runtime error: </span>signed integer overflow: 1912594432 + 1912594432 cannot be represented in type 'int'</span>
    </pre>
    
    
    But something else I notice is that once <tt>FilterInputBytes()</tt> stops growing <tt>objPtr</tt> exponentially, it falls back to growing linearly by a very small amount (58 bytes in this example), with each growth requiring <tt>realloc()</tt>—which even if [http://blog.httrack.com/blog/2014/04/05/a-story-of-realloc-and-laziness/|made much faster by using Linux’s <tt>mremap()</tt>] will still cause throughput to drop dramatically. While the overflow can be avoiding by capping <tt>length</tt> to <tt>INT_MAX</tt>, low-memory systems—or systems with plenty of memory if <tt>FilterInputBytes()</tt> is rewritten to use <tt>size_t</tt> for Tcl 9—still have the slow linear growth performance issue, so it may still be desirable to improve the <tt>objPtr</tt> growth algorithm to minimize the number of <tt>realloc()</tt> calls. One approach I think of is something that approximates logistic growth: if <tt>length = offset + offset</tt> fails, try again with <tt>length = offset + (offset >> 1)</tt>; otherwise try <tt>length = offset + (offset >> 2)</tt>; and so on until whatever reasonable minimum.
    
  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: "24. Channel Commands"
  14. title changed to:
    FilterInputBytes(): signed integer overflow, poor worst-case performance lengthening objPtr
    
  15. type changed to: "Bug"