Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2026-06-30
12:33 • Closed ticket [f04e9b1304]: Crash in Tcl_UtfToUpper/Lower/Title on a genuine 4-b... artifact: 02667bc0c7 user: jan.nijtmans
12:30 • Ticket [f04e9b1304]: 3 changes artifact: 967f9938ba user: jan.nijtmans
12:18
Fix [f04e9b1304]: Crash in Tcl_UtfToUpper/Lower/Title on a genuine 4-byte UTF-8 character check-in: 8fdf2f999c user: jan.nijtmans tags: core-8-6-branch
07:31 • Ticket [f04e9b1304] Crash in Tcl_UtfToUpper/Lower/Title on a genuine 4-byte UTF-... artifact: 092616596c user: oehhar
2026-06-29
14:06 • Ticket [f04e9b1304]: 3 changes artifact: 476081d4ed user: dgp
08:15 • Ticket [f04e9b1304]: 4 changes artifact: f1668d9b4b user: oehhar
2026-06-25
07:42 • Add attachment tcl86-utfcount-nonbmp-crash.patch to ticket [f04e9b13049... artifact: 6159f57da2 user: serhiy.storchaka
07:41 • New ticket [f04e9b1304] Crash in Tcl_UtfToUpper/Lower/Title on a genuine 4-byte ... artifact: 72eaf52288 user: serhiy.storchaka

Crash in Tcl_UtfToUpper/Lower/Title on a genuine 4-byte UTF-8 character

Closed / Fixedf04e9b1304920444eec586455f30c501abb044d0Created 161 days agoUpdated 155 days ago
Descriptionmarkdown2026-06-25 07:41:28

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):

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

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:

#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.

Referenced by1
  • 8fdf2f999cFix [f04e9b1304]: Crash in Tcl_UtfToUpper/Lower/Title on a genuine 4-byte UTF-8 character check-in2026-06-30 12:18:22
oehharwiki2026-06-29 08:15:37

This is 8.6 which is end of life.

Someone may nevertheless apply the patch. Give a sign if you want commit rights to do it on your own.

Thanks for all, Harald

dgpplain2026-06-29 14:06:41
I'm not convinced over commit rights for purpose of continuing development
of Tcl 8.  I don't think fossil has per-branch permissions.  Isn't it a
better idea for folks who want to adopt Tcl 8 to make their own fork
repository?
oehharwiki2026-06-30 07:31:20

I tihnk, it is a good idea to merge patches to core-8-6-branch. It might be helpful for someone... Sergey still puts patches in 8.5...

And the alternative is a github fork what I want to avoid.

Serhiy comes from tkinter and reported, that a lot is still on 8.6, as 9.0 migration just started in new linux distributions.

I find it a good think if we get tkinter people in. They have different interest and don't know fossil, so any help is appreciated.

Thanks for all, Harald

jan.nijtmanswiki2026-06-30 12:30:35

> I tihnk, it is a good idea to merge patches to core-8-6-branch

I have no problem doing that, for trivial fixes like this one.

jan.nijtmanswiki2026-06-30 12:33:01

Fixed [here]

Will (most likely) never land in an official release

Log in (anonymous is fine) to add a comment.