Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2022-04-18
17:22 • Closed ticket [1b1f5f0b53]: Tcl_UtfNext incompatibility in Tcl 8.6.10 plus 4... artifact: e3edf518bf user: jan.nijtmans
2020-04-23
11:37 • Pending ticket [1b1f5f0b53]. artifact: 5e3475f277 user: jan.nijtmans
11:37 • Ticket [1b1f5f0b53]: 3 changes artifact: 15790418b3 user: jan.nijtmans
11:31
Fix [27944a3661]: Taming test utf-6.88 Fix [c11e0c5ce4]: Regression in Tcl_UtfCharComplete Fix [1b1f... check-in: 5c322bbd51 user: jan.nijtmans tags: core-8-6-branch
2020-04-17
15:34 • Ticket [1b1f5f0b53] Tcl_UtfNext incompatibility in Tcl 8.6.10 status still O... artifact: bf8539bf21 user: jan.nijtmans
15:34 • Ticket [1b1f5f0b53]: 3 changes artifact: 7bcb34ef97 user: dgp
14:35 • Ticket [1b1f5f0b53]: 3 changes artifact: 419149266c user: dgp
10:49 • Ticket [1b1f5f0b53]: 3 changes artifact: cd40edafa2 user: jan.nijtmans
03:30 • Ticket [1b1f5f0b53]: 3 changes artifact: eaaede2d91 user: dgp
2020-04-16
21:14 • Ticket [1b1f5f0b53]: 3 changes artifact: a1c0d6ee9f user: dgp
20:31 • Ticket [1b1f5f0b53]: 3 changes artifact: 72afdac540 user: jan.nijtmans
20:06 • Ticket [1b1f5f0b53]: 3 changes artifact: 538c33befb user: jan.nijtmans
2020-04-14
08:49
Fix expecations and test-cases, according to expectations in latest core-8-6-branch (before this ser... check-in: c360caf7c0 user: jan.nijtmans tags: core-8-6-branch
08:44
Fix unit-test, change expectations according to current 8.6 branch (not handling [1b1f5f0b53] yet, d... closed check-in: e64b5a4785 user: jan.nijtmans tags: bug-c61818e4c9
2020-04-13
21:36
Merge 8.5. Failing tests highlight ticket [1b1f5f0b53]. check-in: 50b6d532c4 user: dgp tags: core-8-6-branch
2020-04-05
21:07 • Ticket [1b1f5f0b53] Tcl_UtfNext incompatibility in Tcl 8.6.10 status still O... artifact: 3060cc90b3 user: jan.nijtmans
2020-04-03
18:48 • Ticket [1b1f5f0b53]: 3 changes artifact: 84fd2c113a user: dgp
17:41 • Ticket [1b1f5f0b53]: 3 changes artifact: d93bdfa2bd user: dgp
17:37 • Ticket [1b1f5f0b53]: 3 changes artifact: 36dc0d376e user: dgp
15:29 • Ticket [1b1f5f0b53]: 4 changes artifact: 95a31250ea user: jan.nijtmans
14:58 • New ticket [1b1f5f0b53]. artifact: bf24d4c8c7 user: dgp

Tcl_UtfNext incompatibility in Tcl 8.6.10

Closed / Fixed1b1f5f0b53e243d444628c8f414ea9664b5be44fCreated 7 years agoUpdated 5 years ago
Descriptionplain2020-04-03 14:58:34
Starting with checkin

https://core.tcl-lang.org/tcl/info/aed6634d2c

Tcl_UtfNext() began stepping over 4-byte UTF-8 sequences, even in
the default configuration where TCL_UTF_MAX == 3.  This is inconsistent
new behavior in the default build compared with prior Tcl 8.6 patch releases.

One consequence is that a sequence of steps through a string with
Tcl_UtfNext() no longer matches with the indexing of Tcl_UniCharAtIndex()
Referenced by4
  • 5c322bbd51Fix [27944a3661]: Taming test utf-6.88 Fix [c11e0c5ce4]: Regression in Tcl_UtfCharComplete Fix [1b1f5f0b53]: Tcl_UtfNext incompatibility in Tcl 8.6.10 check-in2020-04-23 11:31:47
  • c360caf7c0Fix expecations and test-cases, according to expectations in latest core-8-6-branch (before this series of fixes). TODO: discussion on [1b1f5f0b53], how to handle invalid 4-byte sequences, which - in fact - are proper UTF-8) check-in2020-04-14 08:49:34
  • e64b5a4785Fix unit-test, change expectations according to current 8.6 branch (not handling [1b1f5f0b53] yet, doing that in separate branch) check-in2020-04-14 08:44:18
  • 50b6d532c4Merge 8.5. Failing tests highlight ticket [1b1f5f0b53]. check-in2020-04-13 21:36:12
jan.nijtmansplain2020-04-03 15:29:52
Actually, I don't think that can actually happen:  Tcl 8.6 never generates 4-byte UTF-8 sequences, it always splits them in two 3-byte UTF-8 sequences internally.
This can only happen if an extension creates a string using Tcl_NewStringObj() which has a 4-byte UTF-8-sequences in it. Earlier Tcl versions handle this as 4 separate bytes, now Tcl tries to handle that more sensible as 2 surrogates. But not everywhere in the code that's handled correctly.

Given that Tcl 8.6 is not designed to handle 4-byte UTF-8 sequences at all, it's a "unspecified behaviour".

That said, I have no problem changing the
   #if TCL_UTF_MAX <= 4
back to
   #if TCL_UTF_MAX == 4

Thanks for noticing this (even though, I don't think you can actually show bad behavior using this with TCL_UTF_MAX=3). Test-case please?
dgpplain2020-04-03 17:37:17
I did try making a test case, but Tcl itself makes very little use of Tcl_UtfNext().
It is used only in the parsing of variable names searching for "(" and "::" characters,
and whether bytes that are not those things are skipped one or four at a time does
no harm.

The impact is only going to be seen in an extension.

A handful of calls are in Tk. I can check those for any scriptlevel demo that
might be possible.
dgpplain2020-04-03 17:41:40
I wish that we could say that handling of non-conformant UTF-8 sequences was "unspecified behavior".

Unfortunately, Tcl's established handling of improper UTF-8 is documented.

http://www.tcl-lang.org/man/tcl8.6/TclLib/Utf.htm
dgpplain2020-04-03 18:48:45
It will take considerable effort to get a final demo, but it looks like
[selection get] with a [selection handle] in place and the wrong values
in the right places are at risk of trouble.
jan.nijtmansplain2020-04-05 21:07:24
I think the documentation should be adapted, explaining why 4-byte sequences are still considered "invalid" in Tcl 8.6, but still are handled internally as if they are formed by 2 surrogates ....  I'll work on that.

Tcl_UtfNext() stepping over 4 bytes is "least surprising" behavior. I don't see how it could cause a problem, given that 4-byte UTF-8 sequences still are "illegal" in 8.6. Stepping over individual bytes (as older Tcl 8.6 releases do) is not usefull behavior at all, I think the complaints on that are valid, even though it - indeed - doesn't match the current documentation.
jan.nijtmanswiki2020-04-16 20:06:59

According to the new documentation in Tcl 8.5:

*In any other circumstance, Tcl_UtfNext returns \fIsrc\fR+1*

That's not according to the original definition of Tcl_UtfNext, and causing an incompatibility! I'll try to come up with a testcase.

jan.nijtmanswiki2020-04-16 20:31:31

Testcase added here: [fd345b2af33581fb]. This has always been wrong, BTW, so (contrary what I wrote before) it is not causing an incompatibility. The new implementation in 8.5 still does it wrong the same way. (Adapting the documentation to conform with the current implementation is too easy ....)

Since Tcl never sets the pointer to the 2nd byte of a 3-byte sequence, I don't think this causes any problem. But if we want Tcl_UtfNext() to behave 100% as originally intended, the we might as well correct this too.

dgpplain2020-04-16 21:14:12
Given the interface restriction, when Tcl_UtfNext gets the argument src,
it cannot know whether it is safe to attempt to read src[-1].

If src points to a trail byte, and we cannot read src[-1] and so on, there's
no way to know whether we're on the 2d or 3d or 4th trail byte or just
pointing to some malformed garbage we still haven't fully outlawed.

Either we do the minimal thing which is step one byte forward always, (current answer,
and the answer we've had in place for 20 years)
or alternatively we could scan forward up to TCL_UTF_MAX-1 bytes and
seek out the first non-trail byte, which would make the new test happier,
but might introduce some synchronization troubles with other assumptions.
dgpplain2020-04-17 03:30:08
The documentation for Tcl_UtfAtIndex(index) has always described it
to be "...equivalent to calling Tcl_UtfNext index times."

When Tcl_UtfNext was changed so that a single call could step over a
four-byte sequence, but Tcl_UtfAtIndex counts indices as if each one
labels a UCS-2 code unit in a UTF-16 representation, that guarantee was
broken.
jan.nijtmanswiki2020-04-17 10:49:33

In commit [7f56d3c4b18e9c6c] you made the following change:

https://core.tcl-lang.org/tcl/fdiff?v1=9b3899b2c5ede87f&v2=5f0549e85e71537a

Do you want me to explain why "\xF4\x90\x80\x80" is an invalid byte sequence, so it's string length should be 4? Or did you figure that out by now? I think it's the last regression in your implementation I'm aware of.

Testcase utf-6.88 shows a long-standing bug IMHO. I'll come back on that later.

My congratulations on the Tcl_Prev()/Tcl_Next() reform: It looks mostly correct now. I don't agree on the documentation change, and there are still 1 or 2 minor corner-cases which are not correct, but we are getting there...

dgpplain2020-04-17 14:35:52
I agree that the documentation is a work in progress. When the timeline of
what is changing and when and why is clear, we'll get better language that
is both accurate and that points the way through the journey.

Should we open a fresh ticket to hash out how Tcl_UtfNext should
act when its argument points to a trail byte?

I don't understand the question about test utf-2.11
It is effectively the same test as utf-2.8 .  It has the same expected result.
What's the confusion?
dgpplain2020-04-17 15:34:19
New tickets: [493dccc2de] [27944a3661]
jan.nijtmansplain2020-04-17 15:34:39
> I don't understand the question about test utf-2.11
> It is effectively the same test as utf-2.8

No, it's not! In utf-2.8, the byte sequence is "\xF0\x90\x80\x80" which translates to Unicode character \U10000, which is perfectly valid. In contrast, utf-2.11, "\xF4\x90\x80\x80", would translate to Unicode character \U110000.  But the maximum Unicode character defined is \U10FFFF, so it's too big. This algorithm, checking the validity of over-long 4-byte sequences is correctly implemented in Tcl_UtfToUniChar(), but it's not correctly implemented in Tcl_UtfNext() yet. I added this test-case to core-8-branch, so you can see that this really is a regression.

Thanks!
jan.nijtmanswiki2020-04-23 11:37:12

Should be fixed in [5c322bbd51d17664]. With this commit, repeated Tcl_UtfNext() calls will make the same steps as repeated Tcl_UtfToUniChar() calls again for valid UTF-8 sequences and (invalid, in case of TCL_UTF_MAX=3) 4-byte UTF-8 sequences.

jan.nijtmans Pending ticket [1b1f5f0b53]. [History diff]2020-04-23 11:37:40
jan.nijtmans Closed ticket [1b1f5f0b53]. [History diff]2022-04-18 17:22:06
Log in (anonymous is fine) to add a comment.