Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 85a32ab58a46814bae4402301aedffad9006491c7595853c410aeb67e6abeddb
Ticket: 1bd796d9c2915891af1afe3f99a3db7bc1ed8a0a
multiple issues in Tcl_DString functions
User & Date: chrstphrchvz on 2022-04-11 21:27:55
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/x-fossil-wiki"
  4. comment changed to:
    There are multiple instances of signed integer overflow in <tt>Tcl_DString</tt> functions. But I am not merely proposing that the overflowing operations be performed as <tt>unsigned int</tt>, because there would still be problematic behavior, such as an infinite loop in <tt>Tcl_ExternalToUtfDString()</tt> during <tt>encoding convertfrom …</tt> in this example (assuming it doesn’t first panic due to insufficient memory):
    
    <pre>
    set f &#91;open /dev/zero &#91;list RDONLY BINARY&#93;&#93;
    fconfigure $f -buffersize &#91;expr {2**20}&#93;
    puts &#91;time {set z &#91;read $f &#91;expr {2**30-1}&#93;&#93;} 1&#93;
    puts &#91;::tcl::unsupported::representation $z&#93;
    puts &#91;time {set a &#91;string cat &#91;encoding convertto ascii {a}&#93; $z&#93;} 1&#93;
    puts &#91;::tcl::unsupported::representation $a&#93;
    puts &#91;time {set s &#91;encoding convertfrom ascii $a&#93;} 1&#93;
    puts &#91;::tcl::unsupported::representation $s&#93;
    </pre>
    
    The infinite loop is due to never being able to grow the destination string large enough for <tt>TableToUtfProc()</tt> to stop returning <tt>TCL_CONVERT_NOSPACE</tt>. There is also overflow wraparound which causes the destination string to be unexpectedly truncated, losing part of the result.
    
    If these sorts of issues are to be resolved (rather than considered won’t-fix for Tcl 8), it seems there would first need to be a decision made on what the intended behaviors are; I’m not sure if I can identify all of them. But this would include deciding what the maximum allowed <tt>length</tt> should be: it seems possible to support <tt>length == INT_MAX</tt> by having Tcl internally treat <tt>spaceAvl</tt> as if it were <tt>unsigned int</tt> (since clients aren’t supposed to use this field anyway) and accommodate <tt>spaceAvl == INT_MAX + 1u</tt>; alternatively, it seems possible to only support up to <tt>length == INT_MAX - 1</tt> through additional <tt>Tcl_Panic()</tt> usage (since the affected functions cannot indicate an error to the caller, and panicking during these functions is already possible when <tt>ckalloc()</tt> or <tt>ckrealloc()</tt> fail). It would also include deciding what to do when a <tt>Tcl_DString</tt> of maximum possible <tt>length</tt> still isn’t large enough to accommodate a result (panicking again seems like an option, since that can already happen due to memory allocation failure).
    
  5. foundin changed to: "8.6.12"
  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: "multiple issues in Tcl_DString functions"
  15. type changed to: "Bug"