Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: c7f205f863cec4d9e71bc5f3394bf7e174bfa59928f930ec1e70111585864bbd
Ticket: 28de50dcee4f6888eaed987bf1d795ac67138b5f
Crash on invalid Unicode code points
User & Date: apnadkarni on 2026-11-25 11:24:43
Changes

  1. icomment:
    The crash was caused by a mismatch between `TclUtfCount` and `Tcl_UniCharToUtf`. The former returned 2 for the number of bytes needed to encode the value because of a broken signed compare, while the latter mapped the invalid value to 0xFFFD which is encoded as 3 bytes. The `ExtendRepWithUnicode` used the former to determine buffer size and the latter to do the actual encoding with obvious consequences. Proposed fix in the bug-28de50dcee branch.
    
    Reviewing the code also revealed another discrepancy.
    
    `TclUtfCount(0x01000061)` returns 3, presumably expecting an encoding of 0xFFFD, while `Tcl_UniCharToUtf` encodes it as a single byte. The effect of this inconsistency will not be as drastic as the crash above but can still have unintended effects. Moreover the behavior of `Tcl_UniCharToUtf` is simply wrong. It maps the illegal code point to an internal TUTF-8 representation of the valid character 'a'.
    
    ```
    % teststringobj newunicode 0  0x01000061
    a
    ```
    
    It is caused by the bit 0x01000000 being treated as the TCL_COMBINE flag which is masked off by `Tcl_UniCharToUtf` resulting in the seen behaviour. This is simply wrong in my view. As far as I can tell, there is no need in Tcl 9 for this flag and it should be removed. Passing a programming flag as part of the datum itself is never a good idea.
    
    While these issues deal with invalid data, they still should not cause crashes or ill behaviour and Tcl should deal with them as consistently as possible.
    
  2. login: "apnadkarni"
  3. mimetype: "text/x-markdown"