Crash in Tcl_UtfToUpper/Lower/Title on a genuine 4-byte UTF-8 character
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.
8fdf2f999cFix [f04e9b1304]: Crash in Tcl_UtfToUpper/Lower/Title on a genuine 4-byte UTF-8 character check-in2026-06-30 12:18:22
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
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?
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
> 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.
Fixed [here]
Will (most likely) never land in an official release