Artifacts Associated With Ticket 7f1162a8674cc65c
Ticket change [7763167afe] (rid 182526) by dgp on 2020-04-26 20:48:40:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/plain"
- comment initialized to:
==== utf-1.14 Tcl_UniCharToUtf: surrogate pairs from concat FAILED ==== Contents of test case: set hi \uD842 set lo \uDC42 eq "$hi$lo" [testbytestring \xF0\xA0\xA1\x92] ---- Result was: 0 ---- Result should have been (exact matching): 1 ==== utf-1.14 FAILED ==== utf-1.15 Tcl_UniCharToUtf: surrogate pairs from concat FAILED ==== Contents of test case: eq [string cat \uD842 \uDC42] [testbytestring \xF0\xA0\xA1\x92] ---- Result was: 0 ---- Result should have been (exact matching): 1 ==== utf-1.15 FAILED - foundin initialized to: "8.7+"
- is_private initialized to: "0"
- login: "dgp"
- priority initialized to: "5 Medium"
- resolution initialized to: "None"
- severity initialized to: "Minor"
- status initialized to: "Open"
- submitter initialized to: "dgp"
- subsystem initialized to: "44. UTF-8 Strings"
- title initialized to: "concatenation does make usable surrogate pairs"
- type initialized to: "Bug"
Ticket change [4302ea6670] (rid 182528) by dgp on 2020-04-26 20:50:21:
- login: "dgp"
- mimetype: "text/plain"
- title changed to: "concatenation does not make usable surrogate pairs"
Ticket change [dbcfcd007d] (rid 183146) by dgp on 2020-05-07 16:24:12:
- icomment:
An analogy to show this is not a new issue, or a new failure. % set x [testbytestring foo\xc0] foo % set y [testbytestring \x80zoo] zoo % string length $x 4 % string length $y 4 % append x $y foozoo % string length $x 8 % string first \u0000 $x 3 % string first \xc0 $x -1 % string first [testbytestring \xc0] $x -1
- login: "dgp"
- mimetype: "text/plain"
- icomment:
Ticket change [8c14f2483d] (rid 183147) by dgp on 2020-05-07 16:27:51:
- icomment:
Continuing... % scan [string index $x 3] %c 192 % scan [string index $x 4] %c 128
- login: "dgp"
- mimetype: "text/plain"
- icomment:
Ticket change [4e8ad1d802] (rid 193267) by jan.nijtmans on 2021-04-07 12:27:40:
- assignee changed to: "jan.nijtmans"
- icomment: "Let's start to handle this one"
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
Ticket change [cea8917268] (rid 193463) by jan.nijtmans on 2021-04-09 09:09:03:
- icomment:
Analysis: Whenever a string starts with a continuation byte, or its first 3 bytes form the (illegal) byte sequence of a lower surrogate, appending this string to another one could lead to undesired side effects, as shown by these test cases. Proposed solution: Do a Unicode (Tcl_UniChar) append in this case. 3 test-cases already fixed this way, but there are more places in the code where this can happen. WIP. The challenge is to find all those places.
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [0669b77091] (rid 193498) by jan.nijtmans on 2021-04-09 15:40:25:
- icomment:
Almost all test-cases fixed now. One left to be done....
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [8e4d9f415a] (rid 193516) by jan.nijtmans on 2021-04-12 10:12:45:
- icomment:
Bug fixed now, and merged to core-8-branch. There is still a bug in TclCompileStringCatCmd() (demonstrated by utf-1.18), therefore compilation of "string cat" is disabled now. Not high prio, since "string cat" is rarely used. Also, there might be other shortcuts possible, where certain code paths might show the same bug. Therefore, not fully closing the ticket yet.
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- resolution changed to: "Fixed"
- status changed to: "Pending"
- icomment:
Ticket change [82b11d063e] (rid 193517) by jan.nijtmans on 2021-04-12 10:56:51:
- icomment:
Demo: <pre> % set x [testbytestring foo\xc0] foo % set y [testbytestring \x80zoo] €zoo % string length $x 4 % string length $y 4 % append x $y fooÀ€zoo % string length $x 8 % string first \u0000 $x -1 % string first \xc0 $x 3 % string first [testbytestring \xc0] $x 3 % scan [string index $x 3] %c 192 % scan [string index $x 4] %c 8364 </pre>
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [bb1a259858] (rid 193518) by sebres on 2021-04-12 13:21:17:
- icomment:
Really Jan?! Are you sure that the result of such utf-string concatenation must be "foozoo" (so dual encoded C080) and not like in every Tcl version before "foo\0zoo"? Imagine you get two strings from somewhere operating with internal tcl-utf string (something acting like a channel with internal utf-encoding like strchan, memchan-similar thing and co. or whatever else generating string in chunks with undefined <b>but fixed</b> length)...<br/> Getting 2 chunks from there where at range of 2 "buffers" would be <code>..C0][80..</code> would either expect some look-ahead (if it is possible at all) or binary reading (if it is possible too) with finally conversion to tcl utf-string (like encoding convertfrom) or some other handling.<br/> Just what is especially brilliant thereby, that the buffer- or chunk-size are extremely crucial now.<br/> So like here: getting a string (again utf-string, not a bytearray) "foo\0zoo" in two chunks with "buffer" size 4 could produce "foozoo" at another end, where doing the same with "buffer" size 8 would get it as "foo\0zoo" as single string.
- login: "sebres"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [72f23fef77] (rid 193519) by sebres on 2021-04-12 13:48:36:
- icomment:
Maybe I just understood it wrong, and this is your screen (or result of copy&paste) that interpreted char <code>\0</code> as `À€`? With other words, what is result of following code in this version? <code><pre style="padding-left:10pt"> tcl::mathop::eq [testbytestring foo\xc0][testbytestring \x80zoo] [testbytestring foo\xc0\x80zoo] </pre></code> I really hope it is 1. Oh, wait, the result of this must be still 7, isn't it? <code><pre style="padding-left:10pt"> % string length [testbytestring foo\xc0\x80zoo] 7 </pre></code> So I really don't understand this anymore.
- login: "sebres"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [1d70bc291d] (rid 193520) by jan.nijtmans on 2021-04-12 14:17:50:
- icomment:
> Really Jan?! Yes, Really!
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [221c986f33] (rid 193521) by jan.nijtmans on 2021-04-12 14:26:24:
- icomment:
@sebres. Just wondering why you want to use "testbytestring" at all in your scripts.
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [aa04bb022b] (rid 193529) by sebres on 2021-04-12 15:51:16:
- icomment:
I don't use testbytestring, OK? But how you would provide an extension to the tcl-level providing functionality working on tcl-string in chunks of fixed block size?.. Something like reflected channel, string/utf-buffer iterators, dynamic converters, etc pp.<br/> Always using internal conversion to/from binary or external encoding?<br/> Then really good luck!
- login: "sebres"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [31a9fbac21] (rid 193530) by jan.nijtmans on 2021-04-12 15:57:33:
- icomment:
Maybe it's helpful to think that this behavior should only be done when appending Tcl strings (so operating on Tcl_Obj's). The Tcl_DString* functions and all channel-functions operate on byte's, those are (and should not) be modified this way!
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [aba0f93c6f] (rid 193532) by jan.nijtmans on 2021-04-12 16:29:14:
- icomment:
OK, let's show the EIAS problem in Tcl 8.6: <pre> % set x [testbytestring foo\xC3] foo % set y [testbytestring \xA9zoz] ©zoz % set z [string replace $y end end o] ©zoo % puts $x$y fooézoz % puts $x$z fooézoo </pre> So $y and $z only differ in their last character. Appending them to $x gives a totally different answer.
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [63ea08caa1] (rid 193533) by sebres on 2021-04-12 16:35:44:
- icomment:
How you'll provide a string (part) to tcl-level? If it is not an <code>Tcl_Obj *</code>?<br/> Or do you simply choice from my examples that what you like (word "channel" without to notice the word "reflected")? It is not always possible to make a look-ahead to fulfill <code>C0</code> (with <code>80</code>) to generate this full \0, for example because <b>it is not flushed yet from other side</b>, or because <b>something is working on fixed block size, so it is still completely impossible to obtain such second pair</b> on this iteration step. So again, how you'd implement and provide such an iterator, for instance from extension to tcl: <code><pre> interate_over_string_from_other_thread part { append s $part # do something with ready parts of $s } </pre></code> - login: "sebres"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [19fbe03eed] (rid 193534) by sebres on 2021-04-12 17:07:59:
- icomment:
As for your example - it is not quite correct, your trying to manipulate at not fullfiled string (by string replace)... A string manipulations over definitely finished part of string (without unfullfiled surrogate) is possible and was always sane in tcl. The simple and sole preventive measure you needed to do was to wait in handler for not a surrogate at end of block, to be able to truncate the "buffer" from start.
- login: "sebres"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [3aede1e9a5] (rid 193576) by pooryorick on 2021-04-12 20:39:24:
- icomment:
Tcl should refuse refuse to work with such invalid data. If a third party produces a Tcl_Obj whose string representation contains something other than Tcl's modified utf-8 encoding, the result of any operation on that string is undefined. Garbage in, garbage out. To that extent, it doesn't matter how the behaviour illustrated in this issue is "fixed". However, code that goes to such efforts to pretend to do something useful with invalid data could confuse future generations of Tcl hackers.
- login: "pooryorick"
- mimetype: "text/plain"
- icomment:
Ticket change [6ef38cdaf2] (rid 193577) by sebres on 2021-04-13 00:03:23:
- icomment:
<i>> Tcl should refuse refuse to work with such invalid data.</i> I have several questions to this statement: <ol> <li>what is exactly valid and invalid in string internal representation?</li> <li>to which time point exactly it becomes not valid? with other words when exactly it should be validated? by call of Tcl_NewStringObj? or may be by something like [/tcl/file?ci=468035ed67ba2ad6&name=generic%2FtclCmdMZ.c&ln=2301|that]: <code><pre> TclNewObj(resultPtr); resultPtr->bytes = string2; resultPtr->length = length2; </pre></code></li> <li>how exactly it is supposed to be refused? Tcl_Panic may be?</li> <li>who decided which method exactly, using which API (I know EIAS, but which one - utf?, byte-array?, unicode?) at C or tcl-level must refuse it?</li> <li>how practicable all that validity stuff will be? (especially considering EIAS, type-less tcl strings, and shimmering on the fly)?</li> <li>and last but not least where it is documented this way and does it not conflict maybe with some API provided by Tcl many many years (for instance as discussed in tclchat reflected channel etc)?</li> </ol> <i>> it doesn't matter how the behaviour illustrated in this issue is "fixed".</i> Well basically it does.<br> Otherwise (if it wouldn't), why just not leave it backwards compatible? :) As for the hackers, I'm really interested to listen the arguments for the case why to consider (an unpaired, single, non-well-formed) byte <code>\x80</code> as an <code>€</code> char (via cp1252) in internal utf representation is less hacking way as to retain it unmodified (as unpaired surrogate, unless it becomes fulfilled or it gets converted somewhere to that what one would expect)? Or why for example not via system encoding? Or why not as <code>Ђ</code> (cp1251)? - login: "sebres"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [a3f64d7dc9] (rid 193589) by jan.nijtmans on 2021-04-13 06:33:48:
- icomment:
> what is exactly valid and invalid in string internal representation? In Tcl 8.6, the internal representation is (modified) [https://en.wikipedia.org/wiki/CESU-8|CESU-8]. In Tcl 8.7 this changed to (modified) [https://simonsapin.github.io/wtf-8/|WTF-8]. The only difference between those two is when handling characters > U+FFFF. Tcl 8.6 any incorrect byte is interpreted as ISO-8859-1. In Tcl 8.7 this changed to CP1252, which is almost the same, except for bytes in the rance 0x80-0x9F. See [https://core.tcl-lang.org/tips/doc/trunk/tip/389.md|TIP #389] > to which time point exactly it becomes not valid? The error-correction is built into the function Tcl_UtfToUniChar(). So whenever the string is interpreted as (unicode) code points instead of bytes. > question 3-6 ... When I wrote TIP #389, I didn't realize that what's described there is actually WTF-8. So the above 3 links should give you everything you need to understand this.
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [dba7571924] (rid 193629) by sebres on 2021-04-13 11:47:03:
- icomment:
Although my questions were rather for Nathan or more to his "Tcl should refuse ...", neither I understant how that what you're describing could help to answer the statement "refuse not-valid string representation", nor how the descriptions of several encodings answer my questions (excepting the first probably, but also there it is questionable). As regards the error-correction in Tcl_UtfToUniChar... Seriously?! Then please read my question again. Not to mention the comprehensibility of the validity check of utf internal representation using some utf to unicode table.
- login: "sebres"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [96798d93d3] (rid 193646) by jan.nijtmans on 2021-04-14 08:30:30:
- icomment:
Closing the ticket now, since the last bug in it is found and fixed. On the "Tcl should refuse" ... Indeed, I think that Tcl should refuse invalid byte sequences. There is an experimental branch for that (encodings-with-flags), compiling this with -DTCL_NO_DEPRECATED (intended for 9.0) will activate the change. I don't think we can introduce that earlier than 9.0, it needs a TIP. Upcoming (I hope). Currently, Tcl doesn't refuse any byte sequence, it just tries to correct it, using CP1252 (or ISO8859-1, for Tcl 8.6) as fallback, when necessary. So your questin (to which time point exactly it becomes not valid?) makes no sense: byte sequences are valid or invalid, as documented by WTF-8 (or CESU-8 for Tcl 8.6). Invalid byte sequences currently are handled as if they are valid, that's something we cannot change in Tcl 8.x. The only think we can improve is the fallback handling conforming to EIAS.
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [22bdb28619] (rid 193647) by jan.nijtmans on 2021-04-14 08:31:02:
- closedate changed to: "2459318.85489558"
- closer changed to: "jan.nijtmans"
- login: "jan.nijtmans"
- mimetype: "text/plain"
- status changed to: "Closed"
Ticket change [1707dcfbc0] (rid 193665) by chw on 2021-04-14 09:38:16:
- closedate changed to: "2459318.90157567"
- closer changed to: "chw"
- icomment:
Hmm, wondering about the comment on top of Tcl_AppendObjToObj() which states ... * IMPORTANT: This routine does not and MUST NOT shimmer appendObjPtr. * Callers are counting on that. ... and later on in the implementation ... /* If appended string starts with a continuation byte or a lower surrogate, * force objPtr to unicode representation. See [7f1162a867] * This fixes append-3.4, append-3.7 and utf-1.18 testcases. */ if (ISCONTINUATION(TclGetString(appendObjPtr))) { Tcl_GetUnicode(objPtr); } ... which has the potential of breaking the promise. Is this not a problem? - login: "chw"
- mimetype: "text/plain"
Ticket change [9ceeb20235] (rid 193666) by jan.nijtmans on 2021-04-14 12:22:11:
- closedate changed to: "2459319.01540631"
- closer changed to: "jan.nijtmans"
- icomment:
> Is this not a problem? Unlikely. There's another asumption: The caller should make sure that appendObjPtr->bytes (if it exists) is valid UTF-8. Shimmering can only happen when 1) appendObjPtr is some other type than string 2) the byte representation of appendObjPtr is not valid UTF-8. So - indeed - garbage in might mean garbage out. I don't think I'm able to create a testcase for that, but be my guest!
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
Ticket change [7e3c15eb74] (rid 193667) by chw on 2021-04-14 14:41:25:
- closedate changed to: "2459319.11210633"
- closer changed to: "chw"
- icomment:
Proof of concept snippet after reading comments in Tcl_JoinObjCmd(): Tcl_Interp *interp; Tcl_UniChar uc[2] = { 0xDE00, 0 }; Tcl_Obj *obj, *cmd; obj = Tcl_NewUnicodeObj(uc, 1); cmd = Tcl_NewListObj(2, NULL); Tcl_ListObjAppendElement(NULL, cmd, obj); Tcl_ListObjAppendElement(NULL, cmd, obj); Tcl_IncrRefCount(cmd); Tcl_JoinObjCmd(NULL, interp, 2, cmd); Tcl_DecrRefCount(cmd); - login: "chw"
- mimetype: "text/plain"
Ticket change [0a36e80471] (rid 193668) by jan.nijtmans on 2021-04-14 15:40:20:
- closedate changed to: "2459319.15301318"
- closer changed to: "jan.nijtmans"
- icomment:
Thank you! I see the point. I think it's solvable ...
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
Ticket change [54242b3336] (rid 193702) by chw on 2021-04-15 08:12:12:
- closedate changed to: "2459319.84180826"
- closer changed to: "chw"
- icomment:
Although check-in [ac4d764e38] tries to address the shimmering issue, I doubt that it is correct. Since shimmering still can happen in ISCONTINUATION(TclGetString(appendObjPtr)) when it has to convert a unicode object to a string.
- login: "chw"
- mimetype: "text/plain"
Ticket change [7bec9363e8] (rid 193703) by jan.nijtmans on 2021-04-15 08:15:20:
- closedate changed to: "2459319.84398679"
- closer changed to: "jan.nijtmans"
- icomment:
Expectation is restored now for Tcl_AppendObjToObj. Thanks, Christian, for bringing this to our attention! This could have led to a crash, indeed.
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
Ticket change [d68cbec15d] (rid 193704) by jan.nijtmans on 2021-04-15 08:17:52:
- closedate changed to: "2459319.84574722"
- icomment:
Shimmering means converting the type to something else, so losing the internal (list) representation. Constructing the byte representation is never dangerous. It's Tcl_GetUnicode() which shimmers, TclGetString() never does that.
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
Ticket change [6efd4d434f] (rid 193714) by dgp on 2021-04-15 11:45:20:
- closedate changed to: "2459319.98982361"
- closer changed to: "dgp"
- icomment:
Test append-3.10 now fails on trunk.
- login: "dgp"
- mimetype: "text/plain"
Ticket change [37d7d1cc4e] (rid 193715) by jan.nijtmans on 2021-04-15 12:52:31:
- closedate changed to: "2459320.03647701"
- closer changed to: "jan.nijtmans"
- icomment:
After a better look, the previous code in Tcl_AppendObjToObj didn't shimmer appendObjPtr at all, contrary to chw's suspicion. It forces objPtr to Unicode format, not appendObjPtr. So the expectation was - after all - not violated at all. Therefore, reverted last change, which was worse because the new code converted appendObjPtr to Unicode always. Thanks, Don and Christian, for always keeping me sharp...
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
Ticket change [516c61862f] (rid 232010) by pooryorick on 2024-08-29 08:24:15:
- closedate changed to: "2460551.8501752"
- closer changed to: "pooryorick"
- icomment:
This report is about byte sequences that are invalid and that Tcl does not itself produce, so if they occur it is because they were introduced by an external source that is breaking Tcl's API contracts. There are currently no tests that exercise the "fix" for this report. <code>ISCONTINUATION</code> does not exist in the [https://core.tcl-lang.org/tcl/timeline?r=unchained|unchained] branch, yet all tests pass. The test suite has diverged somewhat, but utf.test is identical to trunk.
- login: "pooryorick"
- mimetype: "text/x-fossil-wiki"
Ticket change [a133a286d3] (rid 232014) by jan.nijtmans on 2024-08-29 11:10:18:
- closedate changed to: "2460551.96549527"
- closer changed to: "jan.nijtmans"
- foundin changed to: "8.7"
- icomment: "This bug-report is for 8.7 only, not for 9.0"
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
Ticket change [4bd34941f2] (rid 232023) by pooryorick on 2024-08-29 20:16:00:
- closedate changed to: "2460552.34444513"
- closer changed to: "pooryorick"
- icomment:
But <code>ISCONTINUATION() is still in use in the trunk branch, and where it is used, this report is referenced.</code>
- login: "pooryorick"
- mimetype: "text/x-fossil-wiki"