Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 99e07e28d112858e91135e3af73f7c382f3536d2d6fa9570ae6dbfd9e0061c3b
Ticket: ed29806bafd93096f71fb55248258138125805a6
Tcl_UtfToUniChar reads more than TCL_UTF_MAX bytes
User & Date: dgp on 2020-04-27 19:05:44
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/plain"
  4. comment changed to:
    When iterating through a counted (not NUL-terminated) UTF-8 string via calls to
    Tcl_UtfToUniChar(), an important issue is how to keep operations from running
    past the memory approved for use.  Failure to do this might involve transgressing
    memory boundaries, or it might involve reading and acting on uninitialized bits
    found in memory the caller was never given permission to read.
    
    The algorithm embedded in several places in Tcl is to make use of
    Tcl_UtfCharComplete() to check whether a call to Tcl_UtfToUniChar() is
    safe to make with a given number of bytes left in the string.  One key
    invariant that these algorithms rely on is that Tcl_UtfCharComplete(p, n)
    will return true whenever n >= TCL_UTF_MAX.  Callers make use of this
    invariant.  They skip making the Tcl_UtfCharComplete() call that they
    already know will return true, and save those calls only for the tail end
    of the string when there are (TCL_UTF_MAX - 1) or fewer bytes left.
    
    The consequence is that Tcl_UtfToUniChar() should never read more than
    TCL_UTF_MAX bytes.  Starting in Tcl 8.6.10 it is doing so.  When p points to
    a byte in the range 0xF0 - 0xF7, the memory p[3] is read. This eliminates
    the coding guarantee that these iteration tasks are relying on.
    
    These algorithms are in Tcl itself.  There was one in Tcl_NumUtfChars, which
    led to the failure of test utf-4.11 (new since the 8.6.10 release).  The
    Tcl_NumUtfChars routine has been revised on the core-8-6-branch tip to work
    around this broken contract, but that's not a reasonable answer that can be given
    to extensions broken by a patch release change in a public routine.
    
    These algorithms are also in the encoding machinery of channels.  It should
    not be difficult to cook up a demonstration case where this change will
    corrupt the buffer management there, or at least a scenario where mem debug
    or valgrind runs of Tcl will report troubles.
    
    I'm not looking for a quick fix here.  I know this is a difficult issue enmeshed
    in work toward very important new functionality, and toward making Tcl begin
    to offer correctness in string processing that has been missing a long time.
    I'll keep my efforts at a resolution in a feature branch until they can get review.
    
  5. foundin changed to: "8.6.10"
  6. is_private changed to: "0"
  7. login: "dgp"
  8. priority changed to: "5 Medium"
  9. resolution changed to: "None"
  10. severity changed to: "Important"
  11. status changed to: "Open"
  12. submitter changed to: "dgp"
  13. subsystem changed to: "44. UTF-8 Strings"
  14. title changed to: "Tcl_UtfToUniChar reads more than TCL_UTF_MAX bytes"
  15. type changed to: "Bug"