Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Tcl_NumUtfChars not as documented

Closed / Fixed769812ffffffffffffffffffffffffffffffffffCreated 23 years agoUpdated 23 years ago
Descriptionplain2003-07-11 19:20:52
Documentation for Tcl_NumUtfChars says:

     Tcl_NumUtfChars corresponds to strlen for UTF-8
strings.  It
     returns  the  number of Tcl_UniChars that are
represented by
     the UTF-8 string src.  The length of the  source 
string  is
     len  bytes.   If the length is negative, all bytes
up to the
     first NULL byte are used.

However, when length is negative, the routine
does not stop at the first NULL byte.  It stops
after the first UTF sequence that converts to
the null character, notably \xC0\x80

So, it seems that

    Tcl_NumUtfChars("\300\200\000", -1)

will return 0, when it should return 1.

It appears that changing line 506 of revision
1.31 of generic/tclUtf.c from

    if (ch == '\0') {

to

    if (str[-1] == '\0') {

would correct the error.
dgp Ticket [769812ffff]: 1 change [History diff]2003-07-12 04:43:34
hobbsplain2003-07-17 07:23:13
Logged In: YES 
user_id=72656

are there any tests for this?
dgpplain2003-07-17 12:10:13
Logged In: YES 
user_id=80530


The example in the report is
a demo of the bug.

If you're looking for something 
at the script level, try:

package require Tk
pack [message .m -text a\u0000b]

before fixing the bug, only the
character "a" will be displayed.
After the bug fix, all three characters
are displayed.
dgp Ticket [769812ffff]: 2 changes [History diff]2003-10-03 05:29:21
dkf Ticket [769812ffff]: 1 change [History diff]2003-10-03 17:15:13
dkfplain2003-10-08 20:55:35
Logged In: YES 
user_id=79902

Nothing in the Tcl core exhibits the problem; all uses of
Tcl_NumUtfChars also supply the length.  Will need to add
some extra code to tclTest.c to check this...
dkf Closed ticket [769812ffff]. [History diff]2003-10-08 21:28:09
dkfplain2003-10-08 21:28:10
Logged In: YES 
user_id=79902

testing for and fixed in head and 8.4 branch
dkf Ticket [769812ffff]: 2 changes [History diff]2003-10-08 21:28:11
Log in (anonymous is fine) to add a comment.