Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 72eaf522886ab1697b1e5641dd3130c2f116e3b2b6e7d7dd9e41accf937e6088
Ticket: f04e9b1304920444eec586455f30c501abb044d0
Crash in Tcl_UtfToUpper/Lower/Title on a genuine 4-byte UTF-8 character
User & Date: serhiy.storchaka on 2026-06-25 07:41:28
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/x-markdown"
  4. comment changed to:
    ## Problem
    
    `Tcl_UtfToUpper/Lower/Title()` overrun their in-place buffer and crash on a **genuine
    4-byte UTF-8** non-BMP character (not Tcl's internal surrogate-pair form):
    
    ```c
    char buf[64] = "\xF4\x8F\xBF\xBF";   /* U+10FFFF */
    Tcl_UtfToTitle(buf);                 /* SIGSEGV */
    ```
    
    No pure-Tcl reproducer exists — script ingestion decodes to the surrogate-pair form first,
    which is safe. It only bites an embedder that passes canonical UTF-8 to the C API. Tk does
    exactly that when it title-cases a class name during init, which is how CPython crashes:
    `tkinter.Tk(className='\U0010FFFF')` (gh-126219).
    
    ## Cause
    
    `generic/tclUtf.c`. The guard meant to prevent in-place growth is
    
    ```c
    if (len < UtfCount(Char)) memmove(dst, src, len);   /* keep original */
    else dst += TclUCS4ToUtf(Char, dst);                /* write converted */
    ```
    
    For a 4-byte input, `len == 4` but `UtfCount()` returns **3** for a non-BMP character (its
    `return 4` is under `#if TCL_UTF_MAX > 3`), while `TclUCS4ToUtf()` actually writes **6**
    bytes (a surrogate pair). So `4 < 3` is false, 6 bytes are written over the 4-byte slot,
    `dst` overtakes `src`, the NUL is clobbered, and the loop runs off the buffer.
    
    ## Fix
    
    Make `UtfCount()` return the true size in a `TCL_UTF_MAX=3` build:
    
    ```c
    #if TCL_UTF_MAX > 3
        if (((unsigned)(ch - 0x10000) <= 0xFFFFF)) {
    	return 4;
        }
    #else
        if (((unsigned)(ch - 0x10000) <= 0xFFFFF)) {
    	/* A non-BMP character is stored as a 2 x 3-byte surrogate pair. */
    	return 6;
        }
    #endif
        return 3;
    ```
    
    Now `4 < 6` is true: the genuine-4-byte character is passed through unchanged (in-place
    conversion can't grow it safely), while the surrogate-pair form (`len == 6`) still
    converts. No change for `TCL_UTF_MAX > 3`.
    
  5. foundin changed to: "8.6 (`TCL_UTF_MAX=3`, the default)"
  6. is_private changed to: "0"
  7. login: "serhiy.storchaka"
  8. priority changed to: "5 Medium"
  9. private_contact changed to: "c62cfa96d21fcf0294bdb27e141b4604475a3f46"
  10. resolution changed to: "None"
  11. severity changed to: "Important"
  12. status changed to: "Open"
  13. submitter changed to: "serhiy.storchaka"
  14. subsystem changed to: "- New Builtin Commands"
  15. title changed to:
    Crash in Tcl_UtfToUpper/Lower/Title on a genuine 4-byte UTF-8 character
    
  16. type changed to: "Bug"