Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 0ce01f176698410b83e185fca69f3ed78f46227b75548990f3fc11bbe4dbb853
Ticket: b79df322a927dee6517ed87b918b02117d4a8d2e
Tcl_NewUnicodeObj truncates strings
User & Date: apnadkarni on 2022-06-29 14:01:55
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/plain"
  4. comment changed to:
    The Tcl_NewUnicodeObj function truncates passed in strings to half their length.
    
    Culprit is a memcpy of nchars instead of of nchars*sizeof(unsigned short).
    
    However, it is not clear to me that this is the only issue in that module.
    The new String internal rep hosts the string in the unicode[] array, typed
    as Tcl_UniChar. The Tcl_NewUnicodeObj on the other hand manipulates data as
    unsigned short, not Tcl_UniChar. I assume this is because Tcl_NewUnicodeObj
    is intended to deal with UTF-16 in all cases, even when Tcl_UniChar is 4 bytes
    (TCL_UTF_MAX==4). If that is the case, then the line
    
    stringPtr->unicode[numChars] = 0;
    
    will overwrite memory since the allocation is done assuming 2-byte units and 
    sizeof(unicode[0]) is 4 bytes. On the other hand, if Tcl_UniChar is always 2 
    bytes then why is the function defined using unsigned shorts.
    
    I have similar doubts about other usage in the module. For example in 
    SetUTF16StringFromAny (around line 215), the code is allocating in unsigned 
    short units but terminating as an array of Tcl_UniChar.
    
    So also Tcl_SetUnicodeObj. May be others, I did not check the whole module.
    
    I have not provided a patch because I'm not sure if my analysis is correct
    (other than the memcpy fix). Can the String.unicode[] array contain either 
    2-byte or 4-byte units? If so distinguishing using a union may be clearer.
    
  5. foundin changed to: "8.7"
  6. is_private changed to: "0"
  7. login: "apnadkarni"
  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: "apnadkarni"
  13. subsystem changed to: "- New Builtin Object Types"
  14. title changed to: "Tcl_NewUnicodeObj truncates strings"
  15. type changed to: "Bug"