Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2020-04-06
20:56 • Closed ticket [dd010cadfb]: string trim mishandles surrogates plus 5 other... artifact: 8ab627a583 user: jan.nijtmans
20:55 • Ticket [dd010cadfb]: 3 changes artifact: a1b360302d user: jan.nijtmans
20:33
Fix [dd010cadfb]: [string trim] mishandles surrogates check-in: b2cf8c2a88 user: jan.nijtmans tags: core-8-branch
20:08 • Ticket [dd010cadfb] string trim mishandles surrogates status still Open wi... artifact: f4f6555324 user: jan.nijtmans
20:08 • Ticket [dd010cadfb]: 3 changes artifact: fb9e5f9d85 user: jan.nijtmans
15:40 • Ticket [dd010cadfb]: 3 changes artifact: a1a0a45549 user: dgp
15:00 • Ticket [dd010cadfb]: 3 changes artifact: c68630f35b user: jan.nijtmans
14:42 • New ticket [dd010cadfb]. artifact: 383e9a20ab user: dgp

[string trim] mishandles surrogates

Closed / Fixeddd010cadfb99cee6c0a38d1bd4f4927a4945f4ecCreated 7 years agoUpdated 7 years ago
Descriptionplain2020-04-06 14:42:19
Many expectations and invariants are violated.
Trimming a string makes a longer string?
Order of trimming left/right gives different results?

% set orig \U1F4A9
💩
% set remove \U1F4AA
💪
% string length $orig
2
% string length [string trimleft $orig $remove]
3
% string length [string trimright $orig $remove]
0
% string length [string trim $orig $remove]
3
% string length [string trimleft [string trimright $orig $remove] $remove]
0
% string length [string trimright [string trimleft $orig $remove] $remove]
3
Referenced by1
jan.nijtmansplain2020-04-06 15:00:13
So ... just don't do that :-)

"string trim" expects a set of characters as 3th element. Since Emoji are split into 2 surrogates, it starts removing matching surrogates, indeed totally different as expected.

My suggestion: Don't allow Emoji to be used in 3th argument of "string trim"

The same happens when using Emoji between '[' and ']' in regular expressions.

Solution: Switch to Tcl 9.0. Enhance documentation of "string trim" and regsub/regexp explaining this as known limitation.
dgpplain2020-04-06 15:40:33
This "solution" places an input restriction burden on every algorithm 
that relies on  [string trim*] to keep expected invariants in its behavior.

The problem isn't that any programmer would want to do foolish things
like this, it is that the commands are now vulnerable to these inputs in
ways they were not before.

I want people to adopt Tcl 9 too, but I don't want to force the issue
by breaking Tcl 8 and then documenting the breakage.
jan.nijtmansplain2020-04-06 20:08:18
OK, let's do the same with tclsh8.6, on MacOS this time (current core-8-6-branch):

$ tclsh8.6
% set orig \uD83D\uDCA9
💩
% set remove \uD83D\uDCAA
💪
% string length $orig
2
% string length [string trimleft $orig $remove]
1
% string length [string trimright $orig $remove]
2
% string length [string trim $orig $remove]
1
% string length [string trimleft [string trimright $orig $remove] $remove]
1
% string length [string trimright [string trimleft $orig $remove] $remove]
1

This looks more reasonable.  Hm, indeed, something strange is going on. My results on MacOS with 8.7 (current core-8-branch):

$ tclsh8.7
% set orig \uD83D\uDCA9
💩
% set remove \uD83D\uDCAA
💪
% string length $orig
2
% string length [string trimleft $orig $remove]
3
% string length [string trimright $orig $remove]
0
% string length [string trim $orig $remove]
3
% string length [string trimleft [string trimright $orig $remove] $remove]
0
% string length [string trimright [string trimleft $orig $remove] $remove]
3

Same as yours, this result cannot be explained by considering a split into 2 surrogates. Yes, something more weird is going on.    I see your point
jan.nijtmans Ticket [dd010cadfb]: 3 changes [History diff]2020-04-06 20:08:35
jan.nijtmanswiki2020-04-06 20:55:49

Fixed in [b2cf8c2a88]. Solution surprisingly simple.

jan.nijtmans Closed ticket [dd010cadfb]. [History diff]2020-04-06 20:56:07
Log in (anonymous is fine) to add a comment.