Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Handling incomplete byte sequences for utf-16/utf-32

Closed / Fixedd19fe0a5bf54c97217eaba4b28a2c80cf4d16c13Created 4 years agoUpdated 4 years ago
Descriptionwiki2023-02-21 08:36:45
Example:
% encoding convertfrom utf-16le \x41\x00\x41
A

In Tcl 8.7, we see what's happening when using -strict

% encoding convertfrom -strict utf-16le \x41\x00\x00
unexpected byte sequence starting at index 2: '\x0'

But in Tcl 8.6 we don't have -strict, and the byte is silently ignored. That's not good.

Many encodings either output the remaining bytes as-is or the replacement character \xFFFD. For UTF-16/32 outputting the remaining bytes (which easily could be \x00) doesn't make much sense. So most logical would be to output the replacement character in this case (just one).

apnadkarniplain2023-02-21 15:59:26
I had noticed that in my test scripts and adjusted tests accordingly! The premise being that 8.6 was the "gold standard" for 8.x and -profile tcl8 would match that exactly.

/Ashok
jan.nijtmanswiki2023-02-21 16:22:37

I known that 8.6 is the "gold standard", but in this case the 8.6 behavior is not reasonable at all. We could discuss whether \xFFFD or '?' should be output. So, when there is doubt, keep it as-is. I have no doubt that \xFFFD is the 'right' thing to do here.

In this case, silently removing bytes is not described anywhere in the documentation, and no other encoder/decoder does that. So it's 100% defendable that this is a bug in 8.6. Bugs can be fixed, always.

Thanks for noticing this!

jan.nijtmanswiki2023-02-21 17:52:00

Proposed fix [here]. Of course, Tcl 8.6 doesn't have utf-16* and utf-32*, it only has "unicode"

jan.nijtmanswiki2023-02-23 11:17:16

Follow-up [here]

When this fix was merged forward, it turned out that Tcl 8.7 only produces TCL_CONVERT_SYNTAX in -strict mode here. Which doesn't exist in 8.6. Therefore - in Tcl 8.6 - we should always produce \xFFFD in this case for maximum consistancy between Tcl versions.

So, in Tcl 8.6 now:

$ tclsh8.6
% encoding convertfrom unicode \x41\x41\x41
䅁�

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