Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2002-10-08
23:31 • Ticket [619164ffff] Incorrect conversion of \000 using UTF-8 status still Cl... artifact: e882e3bfdd user: wangj1234
16:58 • Closed ticket [619164ffff]. artifact: 3710710930 user: dkf
2002-10-07
23:51 • Ticket [619164ffff]: 1 change artifact: 8662fbb1f4 user: hobbs
2002-10-06
05:47 • New ticket [619164ffff]. artifact: c9c4d0f6c9 user: nobody

Incorrect conversion of \000 using UTF-8

Closed / Invalid619164ffffffffffffffffffffffffffffffffffCreated 24 years agoUpdated 24 years ago
Descriptionplain2002-10-06 05:47:12
When to convert \000 (0x00, \0d00, \0o00), the routtine 
Tcl_UniCharToUtf(ch, str) under tcl/generic/tclUtf.c did not 
process correctly for the case when ch == 0. This code 
segment below is from the latest on the tree when this 
report  is written. 

INLINE int
Tcl_UniCharToUtf(ch, str)
    int ch;/* The Tcl_UniChar to be stored 
in the
 * buffer. */
    char *str;/* Buffer in which the 
UTF-8 representation
 * of the Tcl_UniChar is 
stored.  Buffer must
 * be large enough to 
hold the UTF-8 character
 * (at most 
TCL_UTF_MAX bytes). */
{
    if ((ch > 0) && (ch < UNICODE_SELF)) {
str[0] = (char) ch;
return 1;
    }
    if (ch <= 0x7FF) {
str[1] = (char) ((ch | 0x80) & 0xBF);
str[0] = (char) ((ch >> 6) | 0xC0);
return 2;
    }
    if (ch <= 0xFFFF) {
three:
str[2] = (char) ((ch | 0x80) & 0xBF);
str[1] = (char) (((ch >> 6) | 0x80) & 0xBF);
str[0] = (char) ((ch >> 12) | 0xE0);
return 3;
    }

#if TCL_UTF_MAX > 3
    if (ch <= 0x1FFFFF) {
str[3] = (char) ((ch | 0x80) & 0xBF);
str[2] = (char) (((ch >> 6) | 0x80) & 0xBF);
str[1] = (char) (((ch >> 12) | 0x80) & 0xBF);
str[0] = (char) ((ch >> 18) | 0xF0);
return 4;
    }
    if (ch <= 0x3FFFFFF) {
str[4] = (char) ((ch | 0x80) & 0xBF);
str[3] = (char) (((ch >> 6) | 0x80) & 0xBF);
str[2] = (char) (((ch >> 12) | 0x80) & 0xBF);
str[1] = (char) (((ch >> 18) | 0x80) & 0xBF);
str[0] = (char) ((ch >> 24) | 0xF8);
return 5;
    }
    if (ch <= 0x7FFFFFFF) {
str[5] = (char) ((ch | 0x80) & 0xBF);
str[4] = (char) (((ch >> 6) | 0x80) & 0xBF);
str[3] = (char) (((ch >> 12) | 0x80) & 0xBF);
str[2] = (char) (((ch >> 18) | 0x80) & 0xBF);
str[1] = (char) (((ch >> 24) | 0x80) & 0xBF);
str[0] = (char) ((ch >> 30) | 0xFC);
return 6;
    }
#endif

    ch = 0xFFFD;
    goto three;
}

The code above does not consider correctly the case when 
ch == 0. The case for ch == 0 should be same as the one 
for ((ch > 0) && (ch < UNICODE_SELF)). Also the routine 
UtfCount(ch) just above also have the same problem. 

To reproduce the problem, I just write a simple c extension 
as below,

void testchar(char c) {
  printf("char: %x\n", c);
}

swigged, and load the library inside the tclsh and type 
"testchar \000". If always prints "char: ffffffc0".
hobbs Ticket [619164ffff]: 1 change [History diff]2002-10-07 23:51:11
dkfplain2002-10-08 16:58:32
Logged In: YES 
user_id=79902

This is correct and intentional. Tcl encodes \u0000 as a
multi-byte sequence because this allows (pseudo-)UTF8
strings to be manipulated for many purposes with the
standard C string processing functions, which keeps a lot of
code much simpler.  Without that, you have to start passing
lengths of things *everywhere* and many APIs become a lot
more complex (and just to deal with an extremely rare edge
case.)

Unless someone can exhibit a case where this causes a real
problem, I can't see this as anything that needs further
action.
wangj1234plain2002-10-08 23:31:00
Logged In: YES 
user_id=624034

Thanks a lot for the explanation.
Log in (anonymous is fine) to add a comment.