|
2026-11-27
| ||
| 06:16 | • Ticket [28de50dcee] Crash on invalid Unicode code points status still Closed... artifact: 26caa38a04 user: apnadkarni | |
|
2026-11-26
| ||
| 08:11 | • Ticket [28de50dcee]: 4 changes artifact: 260aba3fce user: jan.nijtmans | |
| 08:03 | • Ticket [28de50dcee]: 5 changes artifact: 9b8a357c32 user: jan.nijtmans | |
| 08:01 | Follow-up to [28de50dcee]: Crash on invalid Unicode code points check-in: 6b393429a4 user: jan.nijtmans tags: core-9-0-branch | |
| 07:46 | Follow-up to [28de50dcee]: Crash on invalid Unicode code points check-in: 2a3ca8cf06 user: jan.nijtmans tags: trunk, main | |
|
2026-11-25
| ||
| 17:26 | • Ticket [28de50dcee] Crash on invalid Unicode code points status still Closed... artifact: a91755aea4 user: apnadkarni | |
| 15:49 | • Ticket [28de50dcee]: 5 changes artifact: 0a0c97356e user: jan.nijtmans | |
| 14:28 | • Ticket [28de50dcee]: 5 changes artifact: 57b5310133 user: apnadkarni | |
| 14:17 | • Closed ticket [28de50dcee]. artifact: f305bc01ec user: jan.nijtmans | |
| 13:57 | Fix [28de50dcee] - crash on invalid code points check-in: 7750fc4113 user: jan.nijtmans tags: core-8-6-branch | |
| 13:50 | Bug [28de50dcee]. Fix crash on invalid code points check-in: af0a748c42 user: jan.nijtmans tags: core-9-0-branch | |
| 13:20 | Fix [28de50dcee] - crash on invalid code points check-in: 89ff76cd67 user: apnadkarni tags: trunk, main | |
| 11:24 | • Ticket [28de50dcee] Crash on invalid Unicode code points status still Open w... artifact: c7f205f863 user: apnadkarni | |
| 08:30 | Bug [28de50dcee]. Fix crash on invalid code points check-in: f9e686b87f user: apnadkarni tags: bug-28de50dcee | |
| 07:10 | • Ticket [28de50dcee] Crash on invalid Unicode code points status still Open w... artifact: ec3791f993 user: apnadkarni | |
| 06:07 | • Ticket [28de50dcee]: 3 changes artifact: da3aaa935a user: apnadkarni | |
| 05:36 | • New ticket [28de50dcee]. artifact: b5d58dfc09 user: apnadkarni | |
Crash on invalid Unicode code points
(Tested on 9.1 but probably present in earlier versions as well)
% set u 0xef1401ef
0xef1401ef
% teststringobj newunicode $u
-> crash on invalid memory access
6b393429a4Follow-up to [28de50dcee]: Crash on invalid Unicode code points check-in2026-11-26 08:01:592a3ca8cf06Follow-up to [28de50dcee]: Crash on invalid Unicode code points check-in2026-11-26 07:46:497750fc4113Fix [28de50dcee] - crash on invalid code points check-in2026-11-25 13:57:12af0a748c42Bug [28de50dcee]. Fix crash on invalid code points check-in2026-11-25 13:50:5989ff76cd67Fix [28de50dcee] - crash on invalid code points check-in2026-11-25 13:20:17f9e686b87fBug [28de50dcee]. Fix crash on invalid code points check-in2026-11-25 08:30:11
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.
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
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.
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.
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).
> 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.
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.
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 [here]
> 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.'
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.