Tcl Source Code

View Ticket
Login
Ticket UUID: 885c86a9a0c537862e5895379d454e652a55324b
Title: convertfrom utf8 breaks for 4 byte utf encodings
Type: Bug Created on: 2023-02-15 11:04:39
Submitter: apnadkarni Assigned to: jan.nijtmans
Subsystem: 16. Commands A-H Severity: Important
Priority: 5 Medium Last modified: 2023-02-22 08:04:55
Status: Closed Closed by: jan.nijtmans
Resolution: Fixed Closed on: 2023-02-22 08:04:55
Version: 8.7
Description:

Consider

% package require Tcl
8.7a6
% codepoints [encoding convertfrom utf-8 \xf0\x80\x80\x80]
U+0000F0 U+0020AC U+000080 U+000080

This should map to U+10000 or at least the equivalent surrogate pair.

User Comments:
jan.nijtmans added on 2023-02-15 11:35:12:

Here, \xf0\x80\x80\x80 is an illegal overlong sequence, so it's split into its bytes. Compare this with Tcl 8.6:

% package require Tcl
8.6.13
% codepoints [encoding convertfrom utf-8 \xf0\x80\x80\x80]
U+0000F0 U+000080 U+000080 U+000080

But now TIP #389 should take it's role, which describes the fallback to cp1252. Therefore, the actual answer should be: U+0000F0 U+0020AC U+0020AC U+0020AC (since byte 0x80 is the euro-sign in cp1252)

Not high prio to fix, since it's an illegal byte sequence anyway. When using -strict it throws an exception:

$ tclsh8.7
% encoding convertfrom -strict utf-8 \xf0\x80\x80\x80
unexpected byte sequence starting at index 0: '\xF0'


apnadkarni added on 2023-02-15 16:19:37:
I shouldn't do UTF arithmetic in my head, I worked it out to U+10000.

Yes, I know it is an overlong sequence but invalid sequences is what I am testing. 

There is no rush with this, I'm just generating test cases to cover as many combinations of invalid sequences that I can from the Unicode standard tables.
Tedious but half the security related bugs in encoders/decoders seem to come from invalid sequences so I don't consider them unimportant.

/Ashok

jan.nijtmans added on 2023-02-15 16:51:05:

> Tedious but half the security related bugs in encoders/decoders seem to come from invalid sequences so I don't consider them unimportant.

Yes, they are important, but the Utf encoders/decoders were already fixed for this possible security issue since 8.6.8 or so. Nothing new for 8.7/9.0

See: [67aa9a2070]


apnadkarni added on 2023-02-15 17:15:44:
Is this cp1252 mapping supposed to happen only when decoding utf-8 or for all encodings (ascii, shiftjis) ? I don't actually see this specified anywhere. TIP 389 only talks about utf<->unichar mapping.

And I assume the C level behaviour is supposed to map to the script level encoding commands?

/Ashok

apnadkarni added on 2023-02-15 17:20:29:
> Yes, they are important, but the Utf encoders/decoders were already fixed for this possible security issue since 8.6.8 or so. Nothing new for 8.7/9.0 

I wasn't talking purely about overlong sequences. In general, misbehavior or bugs with invalid sequence handling in whatever form should not be taken lightly. That is why the standard is so particular about conformant error handling.

/Ashok

jan.nijtmans added on 2023-02-16 07:20:49:

> Is this cp1252 mapping supposed to happen only when decoding utf-8 or for all encodings (ascii, shiftjis) ? I don't actually see this specified anywhere. TIP 389 only talks about utf<->unichar mapping.

... thinking back to long ago .....

According to the TIP it's built into the utf -> unichar encoder, nowhere else. So it should only apply to utf-8 (and cesu-8), not to any other encoders.


jan.nijtmans added on 2023-02-16 21:54:12:

Should be fixed [5627ae0620283c35|here]


jan.nijtmans added on 2023-02-22 08:04:55:

Fixed [c9645172df|here]

Thanks, Ashok, for the report!