Artifacts Associated With Ticket 28de50dcee
Ticket change [b5d58dfc09] (rid 262085) by apnadkarni on 2026-11-25 05:36:04:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/x-markdown"
- comment initialized to:
(Tested on 9.1 but probably present in earlier versions as well) ``` % set u 0xef1401ef 0xef1401ef % teststringobj newunicode $u -> crash on invalid memory access ```
- foundin initialized to: "9.1"
- is_private initialized to: "0"
- login: "apnadkarni"
- priority initialized to: "5 Medium"
- private_contact initialized to: "9546e9877f1a8899e25a89082561ee694e7fb402"
- resolution initialized to: "None"
- severity initialized to: "Important"
- status initialized to: "Open"
- submitter initialized to: "apnadkarni"
- subsystem initialized to: "- New Builtin Commands"
- title initialized to: "Crash on invalid Unicode code points"
- type initialized to: "Bug"
Ticket change [da3aaa935a] (rid 262086) by apnadkarni on 2026-11-25 06:07:02:
- icomment:
Pilot error along with failure of teststringobj to check argument count (it needs an additional argument). Keeping open because I still think there is something strange going on.
- login: "apnadkarni"
- mimetype: "text/x-markdown"
- icomment:
Ticket change [ec3791f993] (rid 262094) by apnadkarni on 2026-11-25 07:10:22:
- icomment:
The following is a corrected version. The crash only shows immediately with memory debugging enabled. ``` % set u 0xef1401ef 0xef1401ef % teststringobj newunicode 0 $u � % teststringobj newunicode 0 $u hi guard byte 0 is 0x0 total mallocs 103338 total frees 93302 current packets allocated 10036 current bytes allocated 645476 maximum packets allocated 10038 maximum bytes allocated 645597 high guard failed at 00000174A9AFE148, c:\src\tcltk\wip\tcl\win\..\generic\tclObj.c 1335 3 bytes allocated at (c:\src\tcltk\wip\tcl\win\..\generic\tclStringObj.c 156) Memory validation failure ```
- login: "apnadkarni"
- mimetype: "text/x-markdown"
- icomment:
Ticket change [c7f205f863] (rid 262103) by apnadkarni on 2026-11-25 11:24:43:
- 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.
- login: "apnadkarni"
- mimetype: "text/x-markdown"
- icomment:
Ticket change [f305bc01ec] (rid 262112) by jan.nijtmans on 2026-11-25 14:17:44:
- assignee changed to: "apnadkarni"
- closedate changed to: "2461370.0956584606"
- closer changed to: "jan.nijtmans"
- icomment:
Thanks for fixing this! I agree with this solution, and merged it to core-9-0-branch and core-8-6-branch too. >... TCL_COMBINE ... As far as I can tell, there is no need in Tcl 9 for this flag The flag TCL_COMBINE is useful if you want to build a UTF-16 decoder using Tcl_UniCharToUtf(). The alternative is a new Tcl_Char16ToUtf(), which shares a lot of code with Tcl_UniCharToUtf(). It didn't seem worth it at the time. Note that this cannot be triggered at pure script-level, only when feeding Tcl_NewUnicodeObj() with invalid UTF-32 data. What could be done additionally is add a check to ExtendStringRepWithUnicode, line 4674, like: <pre> dst += Tcl_UniCharToUtf(((unsigned)unicode[i] < 0x110000) ? unicode[i] : 0xFFFD, dst); </pre> That would also have prevented this crash.
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- resolution changed to: "Fixed"
- status changed to: "Closed"
Ticket change [57b5310133] (rid 262113) by apnadkarni on 2026-11-25 14:28:35:
- closedate changed to: "2461370.1031872109"
- closer changed to: "apnadkarni"
- icomment:
> What could be done additionally is add a check to ExtendStringRepWithUnicode, line 4674, like: That does not fix the actual problem in that TclUtfCount returns the wrong value. > The flag TCL_COMBINE is useful if you want to build a UTF-16 decoder using Tcl_UniCharToUtf() I don't get it. Tcl_UniCharToUtf takes a 32-bit Unichar and outputs UTF-8. Where does UTF-16 come in? Surrogate code points absolutely should not be treated as surrogates in UTF-32 sequences. I really do not like these modifications to the Unicode standard behavior. Different parts of Tcl make differing assumptions which makes it difficult to reason about correct behavior. There *might* have been a need in 8.x. There certainly is not one in Tcl 9 if this is the only use for the flag. I'm going to push for this to be eliminated, particularly as the flag is passed as part of the data, which is unwarranted. Yes, I understand scripts cannot generate invalid encodings. But as I said, we should try and protect against bad data coming in through the C API's as well. Thanks for merging to 9.0 but it only created a fork for me :-) It takes me a while to cherrypick because I repeat the test and valgrind cycle for merges to every branch. (8.6 I more or less ignore).
- login: "apnadkarni"
- mimetype: "text/x-markdown"
Ticket change [0a0c97356e] (rid 262119) by jan.nijtmans on 2026-11-25 15:49:11:
- closedate changed to: "2461370.1591664697"
- closer changed to: "jan.nijtmans"
- icomment:
> I don't get it. Tcl_UniCharToUtf takes a 32-bit Unichar and outputs UTF-8 No, this function takes an _Unichar_. But an unichar could be 16-bit (in Tcl 8.6, in Tcl 9.0 with TCL_UTF_MAX=3 or with TCL_COMBINE) or 32-bit. You don't like the double functionality of this single function, noted! The only difference between those 2 is the handling of surrogates: with TCL_COMINE they are combined, without the flag they are handled as if they were 0xFFFD. As I said, the only way out is a new Tcl_Char16ToUtf() function, which functions as an UTF-16 -> UTF-8 converter one character (or surrogate) at a time.
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
Ticket change [a91755aea4] (rid 262122) by apnadkarni on 2026-11-25 17:26:51:
- closedate changed to: "2461370.2269844329"
- closer changed to: "apnadkarni"
- icomment:
As I said before, I was speaking strictly for 9.x so leaving 8.x out of the way ... > in Tcl 9.0 with TCL_UTF_MAX=3 Is that really an option with Unichar being 16 bits **in Tcl 9**? I find it hard to believe that will work in Tcl 9. It is certainly news to me. If that is the case, I would propose mandating TCL_UTF_MAX=4 for Tcl 9. I can almost guarantee failures if that it not the case across a whole lot of encoding related code.
- login: "apnadkarni"
- mimetype: "text/x-markdown"
Ticket change [9b8a357c32] (rid 262133) by jan.nijtmans on 2026-11-26 08:03:59:
- closedate changed to: "2461370.8361024307"
- closer changed to: "jan.nijtmans"
- icomment:
Another example: <pre> $ teststringobj newunicode 0 0x0100D800 0x0100DC00 \U00010000 </pre> So an invalid UTF-32 sequence of length 2 generated a valid UTF-8 sequence of a different length. That could be another loophole for ill-behavior. Fixed [2a3ca8cf06d496d0|here]
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
Ticket change [260aba3fce] (rid 262134) by jan.nijtmans on 2026-11-26 08:11:55:
- closedate changed to: "2461370.8416117942"
- icomment:
> If that is the case, I would propose mandating TCL_UTF_MAX=4 for Tcl 9 Tcl 9 itself can only be compiled with TCL_UTF_MAX=4. But extensions can compile with -DTCL_UTF_MAX=3, then Tcl_UniChar becomes a short, and (almost) all 'UniChar' API maps to the Char16 equivalent functions. See [https://core.tcl-lang.org/tips/doc/trunk/tip/497.md|TIP #497], the 'Compatibility section': 'An alternative is compile the extension using -DTCL_UTF_MAX=3. This will restore sizeof(Tcl_UniChar) == 2 as visible for the extension. Almost all (non-deprecated) Tcl API's can still be used by the extension, even the ones using Tcl_UniChar, except: Tcl_NewUnicodeObj(), Tcl_SetUnicodeObj(), Tcl_GetUnicodeFromObj(), Tcl_GetUnicode(). There is no need to re-compile Tcl using -DTCL_UTF_MAX=3 for the extension to work.'
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
Ticket change [26caa38a04] (rid 263171) by apnadkarni on 2026-11-27 06:16:46:
- closedate changed to: "2461371.7616519444"
- closer changed to: "apnadkarni"
- icomment:
Jan, As you might expect, I am absolutely opposed to hacks where half the components (the core and some extensions) build with one definition of `Tcl_UniChar` and others build with a different definition, and it all works unless some API's are not used. This is simply not robust. It reminds me of TCHAR's and some components building with -DUNICODE and others without. It just does not make sense.
- login: "apnadkarni"
- mimetype: "text/x-markdown"