Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Isolated surrogates are not replaced when encoding to / from utf-8

Closed / Fixed80d4c1c7e5393907df677e56ccc41bb78ffafe19Created 3 years agoUpdated 3 years ago
Descriptionmarkdown2024-02-06 11:38:47

Isolated (unpaired) surrogates should be treated as encoding errors.

Example:

% binary encode hex [encoding convertto  -profile strict utf-8 \UD800]
unexpected character at index 0: 'U+00D800'

The above is correct as U+D800 should not appear as an isolated surrogate code point. However the following is not,

% binary encode hex [encoding convertto  -profile replace utf-8 \UD800]
eda080

eda080 is the utf-8 for U+D800. The output have been efbfbd (U+FFFD replacement char).

In principle, any time the strict profile raises an error, the replace profile should be substituting the replacement character.

Jan, your thoughts?

Referenced by6
jan.nijtmanswiki2024-02-06 12:18:45

It looks like you forgot the "-profile replace" handling here:

https://core.tcl-lang.org/tcl/file?name=generic/tclEncoding.c&ci=f1cef9d6c96f7ce5&ln=2595-2607

Agreed

jan.nijtmanswiki2024-02-06 12:30:54

Hmmmm. I now see the PROFILE_REPLACE handling just above that ...... I don't know why it's not working as expected then.

jan.nijtmanswiki2024-02-06 13:03:41

Proposed fix [here]

In 8.7 it appears to work as expected, but in 9.0 "-profile replace" handling was lacking for this specific case.

Do you want to add a testcase?

apnadkarniplain2024-02-06 13:15:28
Fix seems reasonable. If you commit it, I'll the test cases.
jan.nijtmanswiki2024-02-06 13:22:55

Committed now

jan.nijtmans Pending ticket [80d4c1c7e5]. [History diff]2024-02-06 13:23:58
apnadkarniplain2024-02-07 03:09:46
Added tests. Closing.
Log in (anonymous is fine) to add a comment.