Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2020-10-26
13:34 • Closed ticket [31aa44375d]: Tcl_NumUtfChars regression in default 8.6 build ... artifact: 180d000b00 user: jan.nijtmans
2020-05-10
13:22 • Ticket [31aa44375d]: 3 changes artifact: c6f2ba5a0a user: jan.nijtmans
2020-05-07
18:27 • Ticket [31aa44375d]: 3 changes artifact: f42e3233ab user: dgp
18:23
New approach to fixing the regression reported in [31aa44375d] builds on recent reforms. Older effo... check-in: ad31bd7310 user: dgp tags: bug-31aa44375d
2020-04-06
11:57 • Ticket [31aa44375d] Tcl_NumUtfChars regression in default 8.6 build status s... artifact: 30eb30840c user: jan.nijtmans
08:02 • Ticket [31aa44375d]: 3 changes artifact: bf231e7773 user: jan.nijtmans
2020-04-05
21:36 • Ticket [31aa44375d]: 3 changes artifact: 0a36a2be70 user: dgp
21:00 • Pending ticket [31aa44375d]. artifact: 065ebad0f9 user: jan.nijtmans
20:28
Partial fix for [31aa44375de2c87e]: Tcl_NumUtfChars regression in default 8.6 build. This commit br... check-in: 166c0270e7 user: jan.nijtmans tags: core-8-6-branch
2020-04-04
13:21 • Ticket [31aa44375d] Tcl_NumUtfChars regression in default 8.6 build status s... artifact: 44a7aaa4df user: dgp
2020-04-03
22:05 • Ticket [31aa44375d]: 3 changes artifact: f8f1d1332c user: dgp
22:02 • Ticket [31aa44375d]: 3 changes artifact: 1859d8df40 user: dgp
21:35 • Ticket [31aa44375d]: 3 changes artifact: 07ed3f8cd0 user: dgp
21:34 • New ticket [31aa44375d]. artifact: 4b704ed541 user: dgp

Tcl_NumUtfChars regression in default 8.6 build

Closed / Fixed31aa44375de2c87ecb1361b15ca0102d831ad155Created 7 years agoUpdated 6 years ago
Descriptionplain2020-04-03 21:34:45
Starting with checkin

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

there is a regression in the function of Tcl_NumUtfChars() in the default build.
Before that checkin:

% lmap i {0 1 2 3 4} {testnumutfchars [testbytestring \xf0\x9f\x92\xa9] $i}
0 1 2 3 4

With that checkin:

% lmap i {0 1 2 3 4} {testnumutfchars [testbytestring \xf0\x9f\x92\xa9] $i}
0 1 2 2 2

Besides the change in results, the new results also suggest that examination
of the buffer is running past the end and reading bytes it should not.
Referenced by1
  • ad31bd7310New approach to fixing the regression reported in [31aa44375d] builds on recent reforms. Older efforts aborted. check-in2020-05-07 18:23:36
dgp Ticket [31aa44375d]: 3 changes [History diff]2020-04-03 21:35:54
dgpplain2020-04-03 22:02:17
Note that in the default TCL_UTF_MAX = 3 build, Tcl_UtfCharComplete
is only demanding 3 bytes when it sees what should be a lead byte for
a 4-byte sequence.

Looks like the consequence is that it is happy knowing 3 bytes are available,
then a call to Tcl_UtfToUniChar actually reads 4 bytes, overrunning the buffer.

At a minimum these two routines have to be in agreement with each other.

This part of the problem may go back farther than the regression.
dgpplain2020-04-03 22:05:20
That change appears to start here:

https://core.tcl-lang.org/tcl/info/a7a44cf3cab2e70f
dgpplain2020-04-04 13:21:24
Note there are also situations (byte two of 4 bytes sequence) where
Tcl_UtfComplete() demands only one byte, but Tcl_UtfToUniChar will
then examine three.

Raising priority due to memory access outside of bounds.
jan.nijtmansplain2020-04-05 21:00:38
Well, first remark:  Tcl 9.0 gives the right answere here:

% lmap i {0 1 2 3 4} {testnumutfchars [testbytestring \xf0\x9f\x92\xa9] $i}
0 1 2 3 1

But ... for Tcl 8.x, we consider 4-byte sequences as consisting on 2 surrogates. So, there the correct answer should be:

% lmap i {0 1 2 3 4} {testnumutfchars [testbytestring \xf0\x9f\x92\xa9] $i}
0 1 2 3 2

You are correctly noticing that Tcl_UtfCharComplete() does not always match with Tcl_UtfToUniChar: Sometimes only 1 byte is demanded, but Tcl_UtfToUniChar() still reads 3 bytes. That's corrected in [166c0270e7812ba4 now. That change changes your test-case into the right answer.

Still, the documentation needs to be adapted explaining why this is the right answer. So, I'm not closing this ticket yet.
dgpplain2020-04-05 21:36:44
Please keep in mind my bug is filed against a default build of the 8.6 branch.

In this ticket I am not complaining about changed behavior in 8.7 and 9.0, or
in custom builds. Only that the default 8.6 should keep doing what it has been doing.

Default 8.6 builds do not know surrogates from a hole in the ground.
jan.nijtmanswiki2020-04-06 08:02:32

So, concluding: the main change you don't agree with is this one:

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

(there were later commits, refining this, but this was the main one)

Well, I created a "bug-31aa44375d" branch, which basically reverts this commit. It means that internal 4-byte UTF-8 sequences (however hard to generate those, they are still officially "invalid") are handled as 4 separate bytes when stepping over it.

Since this change only involved invalid 4-byte sequences (apart from the bug in Tcl_UtfCharComplete() you found, which is now fixed) I didn't think it needed a TIP.

So far, let's see what Travis thinks of it.

jan.nijtmanswiki2020-04-06 11:57:42

See also: a179564826

dgpplain2020-05-07 18:27:13
Fresh start on a bugfix branch building on other reforms and fixes.
jan.nijtmanswiki2020-05-10 13:22:00

As a base for further discussion, I now merged the bug-31aa44375d branch to core-8-6-branch. Testcase encoding-15.5 is now marked as "knownBug" because this is IMHO an important feature: The UtfToUtf encoding is meant to fix all kinds or problems when extenal byte sources find their way into Tcl. A 4-byte UTF-8 sequence is currently illegal in Tcl, but it could be legal to the outside world. Therefore it should be translated to a surrogate pair, so Tcl can handle it as intended. It is possible to fix this using special code in the Utf0toUtf encoder/decoder. This test-case checks for that, and it's broken now. That should be fixed. I'll prepare a solution for that.

So, discussion not over yet, but at least we have a base to talk about.

jan.nijtmanswiki2020-10-26 13:34:09

Remainder of the fix committed here: [2a7beb9d187eff66], together with the fix for [ed29806baf]

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