Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: cd75bcb693bcd3ed0dd0d37794b2f059df366c683681904d67de60438cc7faf1
Ticket: e2eac30e4192a9d26fad9b1771d7b391d7e2592b
incorrect result of convertfrom with 4-byte utf8-characters
User & Date: anonymous on 2022-03-24 11:42:26
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/plain"
  4. comment changed to:
    There is a problem in Tcl_ExternalToUtfDString() (and Tcl_UtfToExternalDString()) when it is called with a string containing more than 50 4-byte UTF-8 characters. Below one can see the buffer content before and after the call to Tcl_ExternalToUtfDString(). In the "after" line, 5 bytes from the end there is an incorrect value of x66
    
    before: (len 204): f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95
     after: (len 204): f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 66 f0 9f 92 95
    
    The problem happens, when the static buffer size of Tcl_DString with 200 bytes (50x 4 bytes) runs out and requires the switch to the dynamically allocated buffer.  
    
    Below is a fix which circumvents the problem by preallocating the Tcl_DString sufficiently long, such that the buffer growing case does not happen in such cases. This is also better performance-wise for inputs with more than 200 bytes, since it avoids a memcpy() for buffer growing. But the provided fix just avoids that the real issue will kick in.
    
    I have learned about a problem by receiving a buffer overflow bug report for NaviServer, where the setup is non-trivial. Below is a very simple plain tcl case showing the problem (in Tcl 8.7a5)
    
    % encoding convertfrom utf-8 [string repeat \xF0\x9f\x92\x95 49]
    πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•
    % encoding convertfrom utf-8 [string repeat \xF0\x9f\x92\x95 51]
    πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•πŸ’•Γ°ΕΈβ€™πŸ’•
    
    With 49 such characters, all is fine. With 51 4-byte chars, one sees the incorrect bytes at the end of the string.
    
    all the best 
    
    -gustaf
    
    
    ============================================================================================================
    --- tclEncoding.c-original	2022-03-13 15:35:55.000000000 +0100
    +++ tclEncoding.c	2022-03-13 15:35:55.000000000 +0100
    @@ -1128,10 +1128,6 @@
         const Encoding *encodingPtr;
         int flags, dstLen, result, soFar, srcRead, dstWrote, dstChars;
     
    -    Tcl_DStringInit(dstPtr);
    -    dst = Tcl_DStringValue(dstPtr);
    -    dstLen = dstPtr->spaceAvl - 1;
    -
         if (encoding == NULL) {
     	encoding = systemEncoding;
         }
    @@ -1143,6 +1139,12 @@
     	srcLen = encodingPtr->lengthProc(src);
         }
     
    +    Tcl_DStringInit(dstPtr);
    +    Tcl_DStringSetLength(dstPtr, srcLen);
    +    dst = Tcl_DStringValue(dstPtr);
    +    dstLen = dstPtr->spaceAvl - 1;
    +    Tcl_DStringSetLength(dstPtr, 0);
    +
         flags = TCL_ENCODING_START | TCL_ENCODING_END;
         if (encodingPtr->toUtfProc == UtfToUtfProc) {
     	flags |= TCL_ENCODING_MODIFIED | TCL_ENCODING_UTF;
    @@ -1320,10 +1322,6 @@
         const Encoding *encodingPtr;
         int flags, dstLen, result, soFar, srcRead, dstWrote, dstChars;
     
    -    Tcl_DStringInit(dstPtr);
    -    dst = Tcl_DStringValue(dstPtr);
    -    dstLen = dstPtr->spaceAvl - 1;
    -
         if (encoding == NULL) {
     	encoding = systemEncoding;
         }
    @@ -1334,6 +1332,13 @@
         } else if (srcLen < 0) {
     	srcLen = strlen(src);
         }
    +
    +    Tcl_DStringInit(dstPtr);
    +    Tcl_DStringSetLength(dstPtr, srcLen);
    +    dst = Tcl_DStringValue(dstPtr);
    +    Tcl_DStringSetLength(dstPtr, 0);
    +    dstLen = dstPtr->spaceAvl - 1;
    +
         flags = TCL_ENCODING_START | TCL_ENCODING_END;
         while (1) {
     	result = encodingPtr->fromUtfProc(encodingPtr->clientData, src,
    ============================================================================================================
    
  5. foundin changed to: "8.7a5"
  6. is_private changed to: "0"
  7. login: "anonymous"
  8. priority changed to: "5 Medium"
  9. private_contact changed to: "ef1993a98c1daa778fe0b246a7af12b3076f2240"
  10. resolution changed to: "None"
  11. severity changed to: "Severe"
  12. status changed to: "Open"
  13. submitter changed to: "anonymous"
  14. subsystem changed to: "- New Builtin Commands"
  15. title changed to:
    incorrect result of convertfrom with 4-byte utf8-characters
    
  16. type changed to: "Bug"