Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2021-03-18
13:33 • Closed ticket [f443140a85]: connection between Tcl_UtfNext/Tcl_UtfPrev and Tcl_U... artifact: 6adab07c4b user: jan.nijtmans
2020-04-29
11:26 • Ticket [f443140a85]: 3 changes artifact: 9e21f1eb6b user: jan.nijtmans
2020-04-28
20:52 • Ticket [f443140a85]: 3 changes artifact: 8deddf1577 user: jan.nijtmans
20:01 • Ticket [f443140a85]: 3 changes artifact: 1e5bb0d442 user: dgp
19:50 • Ticket [f443140a85]: 3 changes artifact: d1a3677699 user: dgp
19:48 • Ticket [f443140a85]: 3 changes artifact: 599a587156 user: jan.nijtmans
19:32 • Ticket [f443140a85]: 3 changes artifact: 3ff2a63f94 user: jan.nijtmans
18:06 • Ticket [f443140a85]: 3 changes artifact: d3857b750b user: dgp
17:49 • Ticket [f443140a85]: 3 changes artifact: 3d8295396a user: dgp
17:38 • Ticket [f443140a85]: 3 changes artifact: e9f5910084 user: dgp
10:15 • Ticket [f443140a85]: 3 changes artifact: a437cb45d4 user: jan.nijtmans
08:29 • Ticket [f443140a85]: 3 changes artifact: 778d02e892 user: jan.nijtmans
2020-04-27
20:49
First shot at implementatio for [f443140a85]. Far from correct yet, since Tcl_UtfPrev() gives strang... check-in: b287415a79 user: jan.nijtmans tags: rfe-f443140a85
18:34 • Ticket [f443140a85] connection between Tcl_UtfNext/Tcl_UtfPrev and Tcl_UtfCharCo... artifact: 3e6efbfe3d user: dgp
16:48 • Ticket [f443140a85]: 3 changes artifact: 74a3ed41ad user: jan.nijtmans
16:48 • New ticket [f443140a85]. artifact: a5247a0e52 user: jan.nijtmans

connection between Tcl_UtfNext/Tcl_UtfPrev and Tcl_UtfCharComplete()

Closed / Fixedf443140a8547b6f6d70c51a6bbf742e72dca0ca8Created 7 years agoUpdated 6 years ago
Descriptionwiki2020-04-27 16:48:09
When hunting for [27944a3661], it has ben apparent that some people expect that Tcl_UtfCharComplete() can be used to protect Tcl_UtfNext() calls for overflowing when the string is not 0-terminated. This has always been true, but not any more with TIP #389. Since such a connection was never documented, no problem: We shouldn't document something that isn't true.

However, there is a problem with TIP #542: According to the current documentation of Tcl_UtfPrev:

When \fIsrc\fR is greater than \fIstart\fR, it
always returns a pointer less than \fIsrc\fR and greater than or
equal to (\fIsrc\fR - \fBTCL_UTF_MAX\fR)

This is not true anymore for Tcl 8.7+. Either change TCL_UTF_MAX to 4 (as they can step over 4 bytes), or make Tcl_UtfNext()/Tcl_UtfPrev() step twice for Characters > 0xFFFF

This ticket is meant to expore the second possibility. If it fails we can always change the documentation. If it works, then Tcl_UtfCharComplete() can be used - again - to protect Tcl_UtfNext calls.

Referenced by1
  • b287415a79First shot at implementatio for [f443140a85]. Far from correct yet, since Tcl_UtfPrev() gives strange results. Tcl_UtfNext() looks OK at first sight. Further testing needed. check-in2020-04-27 20:49:40
jan.nijtmans Ticket [f443140a85]: 3 changes [History diff]2020-04-27 16:48:22
dgpplain2020-04-27 18:34:00
Fully support exploring our options here. Happy we're working through these
rough edges during the alpha development.

One request.  The docs for Tcl_UtfNext have always instructed, "The
caller must not ask for the next character after the last character in
the string if the string is not terminated by a null character."  I think the
inference to use Tcl_UtfCharComplete() as the tool for following this
instruction has been reasonable, but if we're deciding that capability is
dispensable, the documentation should include explicit advice about
what callers of Tcl_UtfNext are supposed to do instead.
jan.nijtmanswiki2020-04-28 08:29:17

I see what's going on: The current documentation can be interpreted various ways, that should be clarified. For example:

test utf-7.40.0 {Tcl_UtfPrev -- overlong sequence}  {testutfprev tip389} {
    testutfprev A\xF0\x90\x80\x80 4
} 3

So, we have a valid 4-byte UTF-8 sequence. The pointer is on the last byte of it.

Currently, the output is "3". Common sense in this case says it should jump to the first byte of the sequence. So, the output should be "1". But ... since - in this branch - we want to make Tcl_UtfPrev() symmetric with Tcl_UtfNext(), it could also make sence to let it jump to the second byte of the sequence so "2".

So, Don, would you expect "1" here (jump to the beginning of the sequence) or "2" (jump to the second byte". I'll have to think on that too.....

jan.nijtmanswiki2020-04-28 10:15:38

Thinking about it, Tcl_UtfPrev() should - whenever possible do to other restrictions - return a pointer to the first byte of the sequence. If TCL_UTF_MAX=3 and Tcl_UtfPrev() encounters 3 trail-bytes, we have the restriction that Tcl_UtfPrev() cannot jump more that TCL_UTF_MAX bytes. So, then it will jump TCL_UTF_MAX bytes, and another call to Tcl_UtfPrev() will be needed to get in sync with the byte sequence again. Something like that should be documented.

Well, accepting the (possible) description above, there are 10 test-cases violating that. Those are:

* utf-7.10.0
* utf-7.10.2
* utf-7.10.4
* utf-7.15.0
* utf-7.15.2
* utf-7.15.4
* utf-7.40.0
* utf-7.41.0
* utf-7.48.2
* utf-7.48.4

All of those involve valid 4-byte sequences where the pointer is not on a character border.

So, still work to do to fix this.

dgpplain2020-04-28 17:38:19
You ask about test utf-7.40.0 .  Note that it is paired with the test utf-7.40.1.
The pair of tests are meant to cover the changing behavior of Tcl through different
releases.

The first test is constrained to "ucs2" which in this case is meant to  indicate a Tcl
interpreter like the default build of Tcl 8.5 where 4 byte sequences are not recognized
as valid at all.  In that circumstance, the byte sequence \xF0\x90\x80\x80 encodes
the character sequence
    U+00F0 U+0090 U+0080 U+0080
and we are pointed at the 4th of those 4 characters.  The correct previous character
is the third one, and the test properly expects the result "3".

We may regret that history, but it is the documented and expected behavior of
our released products.  This test among others will help us not release a Tcl 8.5.20
that is at odds with Tcl 8.5.19.  Folks who cannot abide this nonconformance in
our old release lines are welcome to move forward to the first release line that meets
their standards. We have several to choose from.

The companion test is constrained "fullutf" which in this case is meant to indicate
a Tcl interpreter where 4-byte sequences are recognized.  The expected result of
this test is "1", just as you call for.

I cannot think of any reason why I would hope to see "2" as the answer.

Hmmmm.... now that I look again at your comment, you have a test utf-7.40.0
with a constraint "tip389". That is not correct, and it doesn't match the content of
any of the main dev branches.  Branches 8.5 and 8.6 are as I describe, and
branch 8.7 does not yet have this testing regime brought forward (Still working
on that, sorry). Branches 8.7 and 9 have no test utf-7.40.0 at all yet.

Does that help?
dgpplain2020-04-28 17:49:27
Only some of your followup list of tests match up with the contents of
Tcl 8.5 or 8.6 branches, and in those cases are constrained ucs2.

Looks to me like the only issue here is getting the branches in sync (I am
working on it bit by bit. Trying to be careful. Logging and fixing bugs as I go),
and maybe some work on getting the right constraints in the right places.
dgpplain2020-04-28 18:06:02
Oh boy.  Now I see some of your explorations have been committed to
the 8.5 branch.  Most unwelcome.  Now gotta defensively examine that
and react before I can continue forward.
jan.nijtmansplain2020-04-28 19:32:16
> Now I see some of your explorations have been committed to the 8.5 branch.

Your're welcome to assure yourself that it doesn't have any effect on TCL_UTF_MAX=3.

The TCL_UTF_MAX=4 branch should be kept as much as possible up to date with the "fullutf" feature in later branches: With TCL_UTF_MAX=4 there are no surprises, so whatever tests are devised and whatever bug-fixes are devised in later branches, they all should be harmless to be backported. If not, let me know!
jan.nijtmansplain2020-04-28 19:48:52
B.T.W: I am not happy about your "readlimit" test-cases, utf-6.96 and up. You used it to explore the cases where Tcl_UtfNext() reads more bytes than Tcl_UtfCharComplete(). We already know that there is no relation between those to (I am exploring the possibility to create a relation, because I would like that too).  As soon as you merge those test-cases up to 8.7, it will only mean more meaningless work, keeping the test-cases passing.

Also I'm not happy about removing the --bytestring option:  I want to see clearly in the test-case whether the testcase involves invalid byte-sequences or not. If no, then it would be better to use \xXX \uXXXX or \UXXXXXX syntax. The reason: We cannot forgive changes in handling of valid sequences, but there is some room in invalid sequences (remember the overflow/underflow changes in 4-byte sequences, the really change output, even in Tcl 8.5!)

So, please don't merge testcases utf-6.96 and up to 8.7, better even remove them (and keep them in your own branch if you like).

Apparently I don't understand completely how you name testConstraints, but I'm not very picky on this naming. If you like to rename them, I don't mind much.

Anyway, thanks for your effort streamlining the test-cases in all branches: that's very welcome! I'm trying to the same.
dgpplain2020-04-28 19:50:04
Please forgive the grumpiness in my last comment. Just a reaction to a change
in my plans for the day.

As we can both see now, the changes were largely good ones, some others
mostly harmless, and the few harms easily corrected.
dgpplain2020-04-28 20:01:37
Our two disagreements about [testutfnext] are related.

If I am going to test how callers of Tcl_UtfNext handle the situation
when asked to advance beyond the end of a counted string, I need
the ability to give the testing command a counted string, and that meant
adding another argument, "numBytes".

When I went to add that option, and found the command already with
processing for an optional "--bytestring" it made the task more difficult.
So I asked myself what callers needed this option and when the answer
was that no caller needed this distinction, I eliminated it.

Now that I see your aim is providing an informational clue to the code reader,
I will try to bring something back in to provide that before intruding on
the 8.7 branch  with it.

For now, I am on section 25, not 6, roughly working backwards.
jan.nijtmanswiki2020-04-28 20:52:36

Just a little demo [bb68f27f5b47ad4f]

With a little adaption, "testutfnext" can check whether it overflows the give bytes. For example, try to remove the \x00 from the end of utf-6.9:

==== utf-6.9 Tcl_UtfNext FAILED
==== Contents of test case:
    testutfnext -bytestring \xA0
---- Test generated error; Return code was: 1
---- Return code should have been one of: 0 2
---- errorInfo: Tcl_UtfNext is not supposed to read src[end]
    while executing
"testutfnext -bytestring \xA0"
    ("uplevel" body line 2)
    invoked from within
"uplevel 1 $script"
---- errorCode: NONE
==== utf-6.9 FAILED

This way, we don't need new test-cases with a "numbytes" argument. In the same way, "testutfnext" tests whether src[-1] is read, but we know it doesn't

Hope you like it!

jan.nijtmanswiki2020-04-29 11:26:10

Let me describe a little bit how I see those testcases. For example utf-2.9.[012]

https://core.tcl-lang.org/tcl/artifact?ln=117-125&name=b7a8d0dbc9b5df6e

We want to test "string length" with 4-byte sequences. The "ucs2" selector is used for Tcl 8.5, which doesn't know anything about 4-byte sequences. So test-case utf-2.9.0 handles that.

Builds knowing UCS4 can use the \UXXXXX escape sequence to generate 4-byte sequences. Testcase utf-2.9.1 does that. No need for "testbytestring" here.

Builds not knowing UCS4 can use surrogate pairs. This even works on Tcl 8.5, but there it is actually a different test-case. No problem. This way we have 3 test-cases covering as much situations as possible.

Hope this makes it a little bit more clear

jan.nijtmanswiki2021-03-18 13:33:31

A side effect of TIP #575 is that now - for all versions of Tcl - Tcl_UtfCharComplete() can be used to protect Tcl_UtfNext(). So we can close this ticket.

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