Tcl Source Code

View Ticket
Login
Ticket UUID: d19fe0a5bf54c97217eaba4b28a2c80cf4d16c13
Title: Handling incomplete byte sequences for utf-16/utf-32
Type: Bug Created on: 2023-02-21 08:36:45
Submitter: jan.nijtmans Assigned to: jan.nijtmans
Subsystem: - New Builtin Commands Severity: Minor
Priority: 5 Medium Last modified: 2023-02-23 11:17:16
Status: Closed Closed by: jan.nijtmans
Resolution: Fixed Closed on: 2023-02-23 11:17:16
Version: 8.6
Description:

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).

User Comments:
apnadkarni added on 2023-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.nijtmans added on 2023-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.nijtmans added on 2023-02-21 17:52:00:

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


jan.nijtmans added on 2023-02-23 11:17:16:

Follow-up [548b08e28bde6ab2|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
䅁�