| Ticket UUID: | 28de50dcee4f6888eaed987bf1d795ac67138b5 | ||
| Title: | Crash on invalid Unicode code points | ||
| Type: | Bug | Created on: | 2026-11-25 05:36:04 |
| Submitter: | apnadkarni | Assigned to: | apnadkarni |
| Subsystem: | - New Builtin Commands | Severity: | Important |
| Priority: | 5 Medium | Last modified: | 2026-11-27 06:16:46 |
| Status: | Closed | Closed by: | apnadkarni |
| Resolution: | Fixed | Closed on: | 2026-11-27 06:16:46 |
| Version: | 9.1 | ||
| Description: | ||||
|
(Tested on 9.1 but probably present in earlier versions as well)
| ||||
| User Comments: | ||||
apnadkarni added on 2026-11-25 06:07:02:
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. apnadkarni added on 2026-11-25 07:10:22:
The following is a corrected version. The crash only shows immediately with memory debugging enabled.
apnadkarni added on 2026-11-25 11:24:43:
The crash was caused by a mismatch between Reviewing the code also revealed another discrepancy.
It is caused by the bit 0x01000000 being treated as the TCL_COMBINE flag which is masked off by 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. jan.nijtmans added on 2026-11-25 14:17:44:
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: dst += Tcl_UniCharToUtf(((unsigned)unicode[i] < 0x110000) ? unicode[i] : 0xFFFD, dst); That would also have prevented this crash. apnadkarni added on 2026-11-25 14:28:35:
That does not fix the actual problem in that TclUtfCount returns the wrong value.
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). jan.nijtmans added on 2026-11-25 15:49:11:
> 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. apnadkarni added on 2026-11-25 17:26:51:
As I said before, I was speaking strictly for 9.x so leaving 8.x out of the way ...
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. jan.nijtmans added on 2026-11-26 08:03:59:
Another example: $ teststringobj newunicode 0 0x0100D800 0x0100DC00 \U00010000 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] jan.nijtmans added on 2026-11-26 08:11:55:
> 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 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.' apnadkarni added on 2026-11-27 06:16:46:
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 It reminds me of TCHAR's and some components building with -DUNICODE and others without. It just does not make sense. | ||||
