Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 8497d2b6b76da8ee87ad20a4e20b4681c57abf7f053aed9499b09ac0d03b38d0
Ticket: e9a2715d912d86448a2ed18b2d25788cd50769b4
Tcl 8.6.11: Incompatible Tcl_GetRange()
User & Date: dgp on 2022-03-12 23:33:48
Changes

  1. closedate changed to: "2459651.48180772"
  2. closer changed to: "dgp"
  3. icomment:
    
    I went through the history, and found many things to make me unhappy.
    Bad design made worse by bad documentation.  But I'll spare you a long
    rant at every turn of the rabbit hole.
    
    I made no note of this change in the 8.6.11 release notes because I
    honestly had no idea this functionality existed, let alone someone
    might be using it.  If it had been called to my attention I would have
    considered it a bug.  I truly assumed the added argument checking was just
    formalizing the rules any sensible caller was already following. As
    the comment said "The first and last indices are assumed to be in the
    appropriate range."
    
    That said, it did exist, and it did get use.
    
    I have to caution that the mental model that a value of -1 for the
    "last" argument causes the return of a suffix substring starting at
    index "first" and continuing all the way to the end of the string
    is not reliably correct.  It fails in three ways.
    
    1) Tcl_GetRange(objPtr, 0, -1) returns an empty string, because
    then (last - first + 1) is 0, which is not negative, so the special
    modes of Tcl_NewStringObj() and Tcl_NewUnicodeObj() are not engaged.
    
    2) Since 8.6b1 (2009), Tcl_GetRange processes pure byte array
    values differently.  A value of -1 for "last" can lead to a call to
    Tcl_NewByteArrayObj(bytes, length) with length < 0, which is documented
    as invalid.  In practice, Tcl_NewByteArrayObj() converts to length == 0 and
    the result is an empty value.  This is an EIAS violation, where Tcl_GetRange()
    produces different results on the same value differing only in its
    internals.  The work on the branch tip tries to fix the EIAS violation by
    noticing the negative value for "last" and fixing up the length argument,
    which might work except for...
    
    3) When the routines Tcl_New(String|Unicode)Obj() are passed negative
    "length" or "numChars" arguments, they do not in fact change to a
    "take the whole string" mode.  Precisely, they change to a
    "take everything up to the first NUL" mode.  Both the internal Utf
    and the Tcl_UniChar array representations can contain NUL bytes
    interior to the string values.  We can cook up test cases for both
    scenarios, and then those would be EIAS violations with the revised
    bytearray processing.
    
    I'm ambivalent how best to move forward.  Whatever we settle on we should
    be sure to get documented far more clearly.
    
    I will mention that NUL termination tends to get in the way of adopting
    some alternative data structures.  Doing away with it might open the door
    for allowing substrings and strings to share storage more often for example.
    The Tcl_GetRange() behavior noted here is all wrapped up in that representation.
    Even if we preserve this feature now, I might still seek to eliminate it
    for Tcl 9 to open up more future implementation options.
    
    Finally, I can mention that any code that has been calling
    
        Tcl_GetRange( objPtr, first, -1);
    
    in Tcl releases 8.2.0 through 8.6.10 could be revised to call
    
        Tcl_NewUnicodeObj( Tcl_GetUnicode(objPtr) + first, -1 );
    
    to get the same behavior in all those releases and also unchanged
    in 8.6.11 and later.
    
    Hope that helps.
    
  4. login: "dgp"
  5. mimetype: "text/plain"