Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Incorrect size computed for some multi-byte UTF-8 characters

Closed / Fixed2bcf781c6c42f23a493a63f3b9fa723ceb184408Created 5 years agoUpdated 5 years ago
Descriptionplain2022-02-24 17:21:33
Strings with some multi-byte characters produce wrong size calculation results.  This effects string slicing, and in the severe case, cause Tk to crash with memory corruption.  The Tk crash can occur in an entry box, deleting characters one at a time. The following test case demonstrates incorrect string length results.


set blocks {
    blank ⬜
    black ⬛
    yellow 🟨
    green 🟩    
}

set Ublocks {
    blank \U2B1C
    black \U2B1B
    yellow \U1F7E8
    green \U1F7E9
}

proc xlat {from blocks} {
    set to ""
    foreach f $from {
	append to [dict get $blocks $f]
    }
    return $to
}

set s1 yellow
puts "s1 = '$s1'"
puts "string range s1 0 end-1 = '[string range $s1 0 end-1]'"
set word [list yellow black yellow green green]
set emoji [xlat $word $::Ublocks]
puts "$word -> '$emoji'"
puts "string length -> [string length $emoji]"
puts "string length should be [llength $word]"
set el [split $emoji {}]
puts "split length is [llength $el]"
puts "(joined slice of 0 end-1) = '[join [lrange $el 0 end-1] {}]'"
set e2 [string range $emoji 0 end-1]
puts "e2 (string slice 0 end-1) = '$e2'"
puts "e2 string length -> [string length $e2]"
puts "str len should be -> [expr {[llength $word]-1}]"

set e3 [string range $emoji 0 end-2]
puts "e3 (slice 0 end-2) = '$e3'"
puts "e3 string length -> [string length $e3]"
puts "str len should be -> [expr {[llength $word]-2}]"


set s2 ""
foreach k [dict keys $blocks] {
    scan [dict get $blocks $k] "%c" value
    append s2 [format "\\U%X" $value]
}
jan.nijtmanswiki2022-03-03 08:08:28

Let's see, in Tcl 8.6:

% string length 🟨
2
% llength [split 🟨 {}]
1
% string range 🟨 0 0
???
% string range 🟨 1 1
???
% string index 🟨 0
???
% string index 🟨 1
???

In Tcl 8.7:

% string length 🟨
2
% llength [split 🟨 {}]
1
% string range 🟨 0 0
🟨
% string range 🟨 1 1
% string index 🟨 0
🟨
% string index 🟨 1

In Tcl 9.0:

% string length 🟨
1
% llength [split 🟨 {}]
1
% string range 🟨 0 0
🟨
% string range 🟨 1 1
% string index 🟨 0
🟨
% string index 🟨 1

So, length calculations are equal in Tcl 8.6 and 8.7, there isn't - for sure - any bug there. In Tcl 8.7, 🟨 cannot be split into surrogates anymore, that's the main differences visible at script level. Where Tcl 8.6 gives nonsence results when trying to split emoji, Tcl 8.7 gives at least consistent results.

When Tk crashes because of incorrect length calculations, that's a bug in Tk. In Tk things are more complicated, e.g:

% string length πŸ˜Άβ€πŸŒ«οΈ
6
Many emoji consist of multiple Unicode codepoints. When pressing "delete", those all should be deleted. A solution for this in progress, the "glyph_indexing_2" branch. You will find (hopefully) that with Tcl 8.7 (or 9.0) in combination with Tk "glyph_indexing_2", this problem will be gone.

So, I think this ticket should move to Tk, and "glyph_indexing_2" is a proposed solution. This branch works fine for the entry widget, there are still known problems with the text widget that still needs to be worked out.

Hope this helps

jan.nijtmansplain2022-03-03 08:56:06
Maybe better example (since πŸ˜Άβ€πŸŒ«οΈ still doesn't show as 1 emoji on all browsers):
<pre>
% string length πŸ‘¨β€πŸ¦°
5
</pre>
jan.nijtmans Ticket [2bcf781c6c]: 3 changes [History diff]2022-04-27 20:29:10
jan.nijtmanswiki2022-04-27 20:34:49

Is this bug (as far as the cause is in Tcl) fixed with the full-utf-for-87 branch?

At least all the "string length" for 'yellow' and 'green' gives 1 as expected, not 2 as currently in Tcl 8.6 and 8.7

jan.nijtmanswiki2022-05-17 19:46:05

Since TIP #622 is merged to Tcl 8.7 now, closing.

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