Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 5385a97373207ed9daf4bb9bcf283cdb8604418b444e3dda1995b1badc7e5bc1
Ticket: b3628609ad73a105f0e51435e0e0ac5b8205fad7
concatenating strings is broken when the "bytes" field of a Tcl_Obj contains invalid data and the type is not tclStringType
User & Date: pooryorick on 2023-05-02 15:25:23
Changes

  1. icomment:
    I agree that Tcl should be able to retain efficient functions such as
    <code>TclStringCat()</code>.  I think that in this case it is the test that is
    wrong, not <code>TclStringCat()</code>.  Tcl never stores improperly-encoded
    data in <code>Tcl_Obj.bytes</code>, which is documented to contain data
    encoded in utf-8, with each nul character being represented in its overlong
    utf-8 form, <code>\xC0\x80</code>.  Thus, a function like
    <code>TclStringCat()</code> should be able to assume that in the catenation of
    <code>Tcl_Obj.bytes</code> values there is no change to the interepretation of
    any byte in any of the catenated values.
    
    <code>TclStringCat</code> was competently written with that in mind to achieve
    the following goals:
    
        *  Avoid shimmering & string rep generation.
        *  Produce pure bytearray when possible.
        *  Error on overflow.
    
    In 2021 the commits [fd66e6e20e] and [a7df881994] modified
    <code>TclStringCat()</code> to force the generation of a
    <code>tclStringType</code> internal representation in order to avoid the effect
    of combining two otherwise unrelated incomplete byte sequences into one
    complete sequence.  This change degraded <code>TclStringCat()</code> with
    respect to its stated goals.  The current issue report could probably be
    resolved by further degrading <code>TclStringCat()</code> in a similar way, but
    that would move Tcl further in the wrong direction of supporting incomplete
    values in <code>Tcl_Obj.bytes</code>.
    
    As I stated a couple of years ago in [7f1162a867], any third party that stores
    improperly-encoded data in <code>Tcl_Obj.bytes</code> is creating a
    garbage-in/garbage-out situation, and should not expect the results of any Tcl
    functions thereafter to make sense.  Third parties must use
    <code>Tcl_ExternalTo*</code> functions to produce properly-encoded values.  If
    a third party directly modifies <code>Tcl_Obj.bytes</code> such that the last
    byte sequence for an encoded code point isn't complete, then it should not
    expect Tcl to interpret that sequence as complete.
    
    Either third parties pay a relatively small penalty of properly-encoding values
    at the boundary of Tcl, or Tcl pays a relatively large penalty throughout its
    internal routines, and at a cost of greater code complexity.
    
    Fixed in [2ddc0c3fa9] by allowing <code>TclStringCat()</code> to assume that
    all <code>Tcl_Obj.bytes</code> values can be safely catenated as-is.
    
  2. login: "pooryorick"
  3. mimetype: "text/x-fossil-wiki"