Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2024-08-29
20:16 • Ticket [7f1162a867] concatenation does not make usable surrogate pairs statu... artifact: 4bd34941f2 user: pooryorick
11:10 • Ticket [7f1162a867]: 6 changes artifact: a133a286d3 user: jan.nijtmans
08:24 • Ticket [7f1162a867]: 5 changes artifact: 516c61862f user: pooryorick
2023-05-02
15:25 • Ticket [b3628609ad] concatenating strings is broken when the "bytes" f... artifact: 5385a97373 user: pooryorick
2021-04-20
11:24 • Closed ticket [ec06d0db32]: Tcl_AppendObjToObj and Tcl_GetUnicode lead to Valgr... artifact: 8a163e43ba user: jan.nijtmans
2021-04-15
12:52 • Ticket [7f1162a867] concatenation does not make usable surrogate pairs statu... artifact: 37d7d1cc4e user: jan.nijtmans
11:45 • Ticket [7f1162a867]: 5 changes artifact: 6efd4d434f user: dgp
08:17 • Ticket [7f1162a867]: 4 changes artifact: d68cbec15d user: jan.nijtmans
08:15 • Ticket [7f1162a867]: 5 changes artifact: 7bec9363e8 user: jan.nijtmans
08:12 • Ticket [7f1162a867]: 5 changes artifact: 54242b3336 user: chw
2021-04-14
15:40 • Ticket [7f1162a867]: 5 changes artifact: 0a36e80471 user: jan.nijtmans
14:41 • Ticket [7f1162a867]: 5 changes artifact: 7e3c15eb74 user: chw
12:22 • Ticket [7f1162a867]: 5 changes artifact: 9ceeb20235 user: jan.nijtmans
09:38 • Ticket [7f1162a867]: 5 changes artifact: 1707dcfbc0 user: chw
08:31 • Closed ticket [7f1162a867]. artifact: 22bdb28619 user: jan.nijtmans
08:30 • Ticket [7f1162a867]: 3 changes artifact: 96798d93d3 user: jan.nijtmans
08:14
Complete fix for [7f1162a867]. Re-enable compilation of "string cat" (TclCompileStringCatCmd), since... check-in: 94fc162ec7 user: jan.nijtmans tags: core-8-branch
2021-04-13
11:47 • Ticket [7f1162a867] concatenation does not make usable surrogate pairs statu... artifact: dba7571924 user: sebres
06:33 • Ticket [7f1162a867]: 3 changes artifact: a3f64d7dc9 user: jan.nijtmans
00:03 • Ticket [7f1162a867]: 3 changes artifact: 6ef38cdaf2 user: sebres
2021-04-12
20:39 • Ticket [7f1162a867]: 3 changes artifact: 3aede1e9a5 user: pooryorick
17:07 • Ticket [7f1162a867]: 3 changes artifact: 19fbe03eed user: sebres
16:35 • Ticket [7f1162a867]: 3 changes artifact: 63ea08caa1 user: sebres
16:29 • Ticket [7f1162a867]: 3 changes artifact: aba0f93c6f user: jan.nijtmans
15:57 • Ticket [7f1162a867]: 3 changes artifact: 31a9fbac21 user: jan.nijtmans
15:51 • Ticket [7f1162a867]: 3 changes artifact: aa04bb022b user: sebres
14:26 • Ticket [7f1162a867]: 3 changes artifact: 221c986f33 user: jan.nijtmans
14:17 • Ticket [7f1162a867]: 3 changes artifact: 1d70bc291d user: jan.nijtmans
13:48 • Ticket [7f1162a867]: 3 changes artifact: 72f23fef77 user: sebres
13:21 • Ticket [7f1162a867]: 3 changes artifact: bb1a259858 user: sebres
10:56 • Ticket [7f1162a867]: 3 changes artifact: 82b11d063e user: jan.nijtmans
10:12 • Pending ticket [7f1162a867]. artifact: 8e4d9f415a user: jan.nijtmans
09:26
Fix [7f1162a867]. Don't compile "string cat" any more, since there still is a bug in it, demonstrate... check-in: adfaa5c0d2 user: jan.nijtmans tags: core-8-branch
2021-04-09
15:40 • Ticket [7f1162a867] concatenation does not make usable surrogate pairs statu... artifact: 0669b77091 user: jan.nijtmans
09:09 • Ticket [7f1162a867]: 3 changes artifact: cea8917268 user: jan.nijtmans
2021-04-07
12:27 • Ticket [7f1162a867]: 4 changes artifact: 4e8ad1d802 user: jan.nijtmans
2020-05-07
16:27 • Ticket [7f1162a867]: 3 changes artifact: 8c14f2483d user: dgp
16:24 • Ticket [7f1162a867]: 3 changes artifact: dbcfcd007d user: dgp
2020-04-26
20:50 • Ticket [7f1162a867]: 3 changes artifact: 4302ea6670 user: dgp
20:48 • New ticket [7f1162a867]. artifact: 7763167afe user: dgp

concatenation does not make usable surrogate pairs

Closed / Fixed7f1162a8674cc65c9b72169818badc3186f97678Created 7 years agoUpdated 2 years ago
Descriptionplain2020-04-26 20:48:40
==== 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
Referenced by4
  • 5385a97373Ticket [b3628609ad] concatenating strings is broken when the "bytes" field of a Tcl_Obj contains invalid data and the type is not tclStringType status still Open with 3 other changes ticket2023-05-02 15:25:23
  • 8a163e43baClosed ticket [ec06d0db32]: Tcl_AppendObjToObj and Tcl_GetUnicode lead to Valgrind "Invalid read of size". plus 7 other changes ticket2021-04-20 11:24:00
  • 94fc162ec7Complete fix for [7f1162a867]. Re-enable compilation of "string cat" (TclCompileStringCatCmd), since it now works again. check-in2021-04-14 08:14:23
  • adfaa5c0d2Fix [7f1162a867]. Don't compile "string cat" any more, since there still is a bug in it, demonstrated by utf-1.18 testcase check-in2021-04-12 09:26:27
dgp Ticket [7f1162a867]: 3 changes [History diff]2020-04-26 20:50:21
dgpplain2020-05-07 16:24:12
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
dgpplain2020-05-07 16:27:51
Continuing...

% scan [string index $x 3] %c
192

% scan [string index $x 4] %c
128
jan.nijtmanswiki2021-04-07 12:27:40

Let's start to handle this one

jan.nijtmanswiki2021-04-09 09:09:03

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.

jan.nijtmanswiki2021-04-09 15:40:25

Almost all test-cases fixed now. One left to be done....

jan.nijtmanswiki2021-04-12 10:12:45

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.

jan.nijtmanswiki2021-04-12 10:56:51

Demo:

% 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
-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

sebreswiki2021-04-12 13:21:17

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 but fixed length)...
Getting 2 chunks from there where at range of 2 "buffers" would be ..C0][80.. 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.
Just what is especially brilliant thereby, that the buffer- or chunk-size are extremely crucial now.
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.

sebreswiki2021-04-12 13:48:36

Maybe I just understood it wrong, and this is your screen (or result of copy&paste) that interpreted char \0 as `À€`? With other words, what is result of following code in this version?

tcl::mathop::eq [testbytestring foo\xc0][testbytestring \x80zoo] [testbytestring foo\xc0\x80zoo]
I really hope it is 1.

Oh, wait, the result of this must be still 7, isn't it?

% string length [testbytestring foo\xc0\x80zoo]
7
So I really don't understand this anymore.

jan.nijtmanswiki2021-04-12 14:17:50

> Really Jan?!

Yes, Really!

jan.nijtmanswiki2021-04-12 14:26:24

@sebres. Just wondering why you want to use "testbytestring" at all in your scripts.

sebreswiki2021-04-12 15:51:16

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.
Always using internal conversion to/from binary or external encoding?
Then really good luck!

jan.nijtmanswiki2021-04-12 15:57:33

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!

jan.nijtmanswiki2021-04-12 16:29:14

OK, let's show the EIAS problem in Tcl 8.6:

% 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

So $y and $z only differ in their last character. Appending them to $x gives a totally different answer.

sebreswiki2021-04-12 16:35:44

How you'll provide a string (part) to tcl-level? If it is not an Tcl_Obj *?
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 C0 (with 80) to generate this full \0, for example because it is not flushed yet from other side, or because something is working on fixed block size, so it is still completely impossible to obtain such second pair on this iteration step.

So again, how you'd implement and provide such an iterator, for instance from extension to tcl:

interate_over_string_from_other_thread part {
  append s $part
  # do something with ready parts of $s
}

sebreswiki2021-04-12 17:07:59

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.

pooryorickplain2021-04-12 20:39:24
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.
sebreswiki2021-04-13 00:03:23

> Tcl should refuse refuse to work with such invalid data.

I have several questions to this statement:

  1. what is exactly valid and invalid in string internal representation?
  2. 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 that:
        TclNewObj(resultPtr);
        resultPtr->bytes = string2;
        resultPtr->length = length2;
    
  3. how exactly it is supposed to be refused? Tcl_Panic may be?
  4. 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?
  5. how practicable all that validity stuff will be? (especially considering EIAS, type-less tcl strings, and shimmering on the fly)?
  6. 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)?

> it doesn't matter how the behaviour illustrated in this issue is "fixed".

Well basically it does.
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 \x80 as an € 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 Ђ (cp1251)?

jan.nijtmanswiki2021-04-13 06:33:48

> what is exactly valid and invalid in string internal representation?

In Tcl 8.6, the internal representation is (modified) CESU-8. In Tcl 8.7 this changed to (modified) 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 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.

sebreswiki2021-04-13 11:47:03

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.

jan.nijtmanswiki2021-04-14 08:30:30

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.

jan.nijtmans Closed ticket [7f1162a867]. [History diff]2021-04-14 08:31:02
chwplain2021-04-14 09:38:16
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?
jan.nijtmanswiki2021-04-14 12:22:11

> 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!

chwplain2021-04-14 14:41:25
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);
jan.nijtmanswiki2021-04-14 15:40:20

Thank you! I see the point. I think it's solvable ...

chwplain2021-04-15 08:12:12
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.
jan.nijtmanswiki2021-04-15 08:15:20

Expectation is restored now for Tcl_AppendObjToObj. Thanks, Christian, for bringing this to our attention! This could have led to a crash, indeed.

jan.nijtmanswiki2021-04-15 08:17:52

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.

dgpplain2021-04-15 11:45:20
Test append-3.10 now fails on trunk.
jan.nijtmanswiki2021-04-15 12:52:31

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...

pooryorickwiki2024-08-29 08:24:15

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. ISCONTINUATION does not exist in the unchained branch, yet all tests pass. The test suite has diverged somewhat, but utf.test is identical to trunk.

jan.nijtmanswiki2024-08-29 11:10:18

This bug-report is for 8.7 only, not for 9.0

pooryorickwiki2024-08-29 20:16:00

But ISCONTINUATION() is still in use in the trunk branch, and where it is used, this report is referenced.

Log in (anonymous is fine) to add a comment.